首页 > Web开发 > 详细

Node js实践操作

时间:2020-04-09 14:22:29      阅读:71      评论:0      收藏:0      [点我收藏+]
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

Node js实践操作

原文:https://www.cnblogs.com/MDGE/p/12666540.html

(1)
(1)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!