express/koa 总结

1.app.js 

app.use('/', indexRouter);
app.use('/users', usersRouter);   // 追踪usersRouter,注意 /users 路径,当然也可以任意修改,相应的以下前端请求路径也要跟着变化


// 追踪usersRouter
// node服务器接收的前端get请求路径 http://localhost:3000/users + 以下路径
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

 2.服务器接收到的参数

router.get('/login', function(req, res, next) {
  let query = req.query; // 前端传过来的参数  
  res.send(query); // 返回你自己定义
});

 3.本地页面打开express 中跨域 axios 请求

app.get('/api/newsdata', async (req, res) => {
  // 解决跨域
  res.append("Access-Control-Allow-Origin", "*")
  res.append("Access-Control-Allow-content-type": "*")
  let result = await axios.get('https://i.snssdk.com/forum/home/v1/info/?balabala...');
  res.send(result.data);
})
app.listen(8080, () => { console.log("server Start:") console.log("http://localhost:8080/api/newsdata") // 就是这个!! })

 4.为了获取更准确的时间,建议使用 process.hrtime()

posted @ 2019-11-22 11:33  blackatall  阅读(257)  评论(0编辑  收藏  举报