我写的一坨大便的答案(没有包含MVC设计模式)
shop.ts
import * as http from 'http';
const axios=require('axios')
const express=require('express')
// websocket,tcp,socket.io
const socketio=require('socket.io')
const fs = require('fs');
const path = require('path');
const jwt=require('jsonwebtoken')
const {expressjwt} = require('express-jwt');
const app=express();
const server = http.createServer(app)
const bodyParser = require('body-parser');
const io=socketio(server,{
path:'/socket.io/',
pingTimeout: 1000 * 60,//超时时间
pingInterval: 1000 * 1,//ping的时间
transports: ['websocket', 'polling'],//传输方式
allowUpgrades: true,//传输是否升级
httpCompression: true,//是否加密
allowEIO3: true,
cors:{
origin: "https://127.0.0.1:8080", // 必须和客户端请求的 origin 一致
methods: ["GET", "POST"],
credentials: true
},
connectionStateRecovery: {
// the backup duration of the sessions and the packets
maxDisconnectionDuration: 2 * 60 * 1000,
// whether to skip middlewares upon successful recovery
skipMiddlewares: true,
}
});
//测试用数据添加
import { ShopDataStructure } from './shopdatastructure';
let data_structure=
[
{ id: 1, name: "Chicken Wing", category: "Food", qty: 3, price: 10 },
{ id: 2, name: "Pizza", category: "Food", qty: 1, price: 50 },
{ id: 3, name: "Hamburger", category: "Food", qty: 1, price: 12 },
{ id: 4, name: "Coca Cola", category: "Drink", qty: 2, price: 5 },
{ id: 5, name: "Orange Juice", category: "Drink", qty: 1, price: 15 },
{ id: 6, name: "Potato Chips", category: "Snack", qty: 1, price: 8 },
{ id: 7, name: "tomato", category: "Vegetable", qty: 1, price: 2 },
]
let shopdatastructure=new ShopDataStructure();
shopdatastructure.InitShopDataStructure(data_structure)
app.get('/all',(req:any,res:any)=>{
let alldata=shopdatastructure.GetIdAllData()//一个map
let returndata:any[]=[];
for(let data of alldata)
{
returndata.push(data);
}
res.send({alldata:returndata});
})
app.get('/all/:id',(req:any,res:any)=>{
const id = parseInt(req.params.id);
console.log(id)
let alldata=shopdatastructure.GetIdAllData()//一个map
console.log(alldata.get(id))
res.send({iddata:alldata.get(id)})
})
app.get('/:category/:id',(req:any,res:any)=>{
const id = parseInt(req.params.id);
let alldata=shopdatastructure.GetIdAllData();
let shopdata=alldata.get(id)
console.log(shopdata?.category,req.params.category)
if(shopdata?.category==req.params.category)
{
res.send({category_id_data:shopdata})
}
})
app.get('/food',(req:any,res:any)=>{
let alldata=shopdatastructure.GetCategoryData();
let shopdata=alldata.get("Food")
res.send({food_data:shopdata})
})
app.get('/drink/:id',(req:any,res:any)=>{
const id = parseInt(req.params.id);
let alldata=shopdatastructure.GetIdAllData();
let shopdata=alldata.get(id)
if(shopdata?.category=="drink")
{
res.send({category_id_data:shopdata})
}
})
server.listen('8080', () => {
console.log('listen');
});
shopdata.ts
export class ShopData{
public id:number=0;
public name:string="";
public category:string="";
public qty:number=0;
public price:number=0;
constructor(id:number,name:string,category:string,qty:number,price:number)
{
this.id=id
this.name=name
this.category=category
this.qty=qty
this.price=price
}
}
shopdatastructure.ts
import { ShopData } from "./shopdata";
export class ShopDataStructure {
public shopidmap: Map<number, ShopData> = new Map<number, ShopData>();
public shopcategorymap: Map<string, ShopData[]> = new Map<string, ShopData[]>();
constructor() {}
InitShopDataStructure(data_array: any) {
for (let t_data of data_array) {
let shopdata = new ShopData(
t_data.id,
t_data.name,
t_data.category,
t_data.qty,
t_data.price
);
this.setshopdata(shopdata);
}
console.log(this.shopidmap)
console.log(this.shopcategorymap)
}
GetIdAllData() {
return this.shopidmap;
}
GetCategoryData() {
return this.shopcategorymap;
}
setshopdata(shopdata: ShopData) {
// 先将商店数据加入 shopidmap
this.shopidmap.set(shopdata.id, shopdata);
// 获取对应类别的商店数组,如果不存在则初始化为空数组
let tcategory = this.shopcategorymap.get(shopdata.category);
if (!tcategory) {
tcategory = []; // 初始化为空数组
}
// 将新商店数据推入该类别数组中
tcategory.push(shopdata);
// 更新类别映射
this.shopcategorymap.set(shopdata.category, tcategory);
}
}
很烦第一次参加笔试,想写多点东西,反应过来的时候时间就不够了,写了依托答辩,没测试就交了第一次邮件,后面看了一下好像忘记id是number来着,艹果然写出bug了,又交了第二次,然后又忘记发简历
后面又突然发现没按MVC设计模式做哈哈哈寄了,菜就多练
更改后的答案
Moudel,shopdataModel.ts,shopdata.ts
**shopdataModel.ts**
import { ShopData } from "./shopdata";
export class ShopDataStructure {
public shopidmap: Map<number, ShopData> = new Map<number, ShopData>();
public shopcategorymap: Map<string, ShopData[]> = new Map<string, ShopData[]>();
constructor() {}
InitShopDataStructure(data_array: any) {
for (let t_data of data_array) {
let shopdata = new ShopData(
t_data.id,
t_data.name,
t_data.category,
t_data.qty,
t_data.price
);
this.setshopdata(shopdata);
}
console.log(this.shopidmap)
console.log(this.shopcategorymap)
}
GetIdAllData() {
return this.shopidmap;
}
GetCategoryData() {
return this.shopcategorymap;
}
setshopdata(shopdata: ShopData) {
// 先将商店数据加入 shopidmap
this.shopidmap.set(shopdata.id, shopdata);
// 获取对应类别的商店数组,如果不存在则初始化为空数组
let tcategory = this.shopcategorymap.get(shopdata.category);
if (!tcategory) {
tcategory = []; // 初始化为空数组
}
// 将新商店数据推入该类别数组中
tcategory.push(shopdata);
// 更新类别映射
this.shopcategorymap.set(shopdata.category, tcategory);
}
}
**shopdata.ts**
export class ShopData{
public id:number=0;
public name:string="";
public category:string="";
public qty:number=0;
public price:number=0;
constructor(id:number,name:string,category:string,qty:number,price:number)
{
this.id=id
this.name=name
this.category=category
this.qty=qty
this.price=price
}
}
Control,shopcontrol.ts
import { Request, Response } from 'express';
import {ShopDataStructure} from './shopdataModel'
let data_structure=
[
{ id: 1, name: "Chicken Wing", category: "Food", qty: 3, price: 10 },
{ id: 2, name: "Pizza", category: "Food", qty: 1, price: 50 },
{ id: 3, name: "Hamburger", category: "Food", qty: 1, price: 12 },
{ id: 4, name: "Coca Cola", category: "Drink", qty: 2, price: 5 },
{ id: 5, name: "Orange Juice", category: "Drink", qty: 1, price: 15 },
{ id: 6, name: "Potato Chips", category: "Snack", qty: 1, price: 8 },
{ id: 7, name: "tomato", category: "Vegetable", qty: 1, price: 2 },
]
let shopdatastructure=new ShopDataStructure();
shopdatastructure.InitShopDataStructure(data_structure)
export const getall=(req:Request,res:Response)=>{
let alldata=shopdatastructure.GetIdAllData()//一个map
let returndata:any[]=[];
for(let data of alldata)
{
returndata.push(data);
}
res.send({alldata:returndata});
}
export const getuse_id=(req:Request,res:Response)=>{
const id = parseInt(req.params.id);
console.log(id)
let alldata=shopdatastructure.GetIdAllData()//一个map
console.log(alldata.get(id))
res.send({iddata:alldata.get(id)})
}
export const getuse_category_id=(req:Request,res:Response)=>{
const id = parseInt(req.params.id);
let alldata=shopdatastructure.GetIdAllData();
let shopdata=alldata.get(id)
console.log(shopdata?.category,req.params.category)
if(shopdata?.category==req.params.category)
{
res.send({category_id_data:shopdata})
}
}
export const getuse_food=(req:any,res:any)=>{
let alldata=shopdatastructure.GetCategoryData();
let shopdata=alldata.get("Food")
res.send({food_data:shopdata})
}
export const getuse_drink_id=(req:Request,res:Response)=>{
const id = parseInt(req.params.id);
let alldata=shopdatastructure.GetIdAllData();
let shopdata=alldata.get(id)
if(shopdata?.category=="drink")
{
res.send({category_id_data:shopdata})
}
}
Routes,shoproutes.ts
import { Router } from "express";
import * as col from "./shopcontrol"
export const router=Router()
router.get('/all',col.getall)
router.get('/all/:id',col.getuse_id)
router.get('/:category/:id',col.getuse_category_id)
router.get('/food',col.getuse_food)
router.get('/drink/:id',col.getuse_drink_id)
shop.ts
import * as http from 'http';
const express=require('express')
const app=express();
const server = http.createServer(app)
import {router} from './shoproutes'
app.use(router)
server.listen('8080', () => {
console.log('listen');
});
这样子的写法才相对正确一点,总的来说是一次不错的考核吧,比较基础的,但是我确实在nodejs设计模式方面好像还是不太行,也确实菜哈哈哈,不过现在学到了,机会是留给有准备的人的,希望看到这篇文章的各位以我为鉴,说实话我的nodejs水平在笔试之前还停留在6月份那会
评论已关闭