Node js实践操作

const express = require('express');

const app = express()
先介绍一个中间件开发中经常碰到跨域问题 cors,通过use来使用中间件

const cors = require('cors'); 
app.use(cors())
那我我们正常去使用js的时候就是node  **.js,但是我们还可以 app.use(express.static('html')),当我们运行这个的时候找的自然就是我们html文件夹中的index.html,
但我们使用了app.route('/info')
.get(function(req,res){
  res.send('hhhhh')
})
我们可以写多个不同的方法来使用这个info接口当然请求方式不一样
那我们还可以有一种方式就是用路由
var birds = require('./routers/birds.js');
app.use('/birds',birds)
在birds.js里面我们首先要引用
var express = require('express');
var router = express.Router();  
然后我们通过use做一层过滤操作
router.use(function(req,res,next){
  console.log(res)
  next()
})
router.get('/home',function(req,res){
  res.json('我是home')
})
router.get('/about',function(req,res){
  res.json('我是about')
})

module.exports = router
router 就是birds
原文地址:https://www.cnblogs.com/MDGE/p/12666540.html