一个简单的node.js
2021-03-09 15:30
标签:set add app 一个 utf-8 fun server cat func var express = require(‘./node_modules/express‘) 一个简单的node.js 标签:set add app 一个 utf-8 fun server cat func 原文地址:https://www.cnblogs.com/guokt/p/12863084.html
var app = express()
// 设置跨域访问
app.all(‘*‘, function(req, res, next) {
res.header(‘Access-Control-Allow-Origin‘, ‘*‘)
res.header(‘Access-Control-Allow-Headers‘, ‘X-Requested-With‘)
res.header(‘Access-Control-Allow-Methods‘, ‘PUT,POST,GET,DELETE,OPTIONS‘)
res.header(‘Content-Type‘, ‘application/json;charset=utf-8‘)
next()
})
var data = [
{ name: ‘lisi‘, age: 20 },
{ name: ‘wangwu‘, age: 30 }
]
// 写个接口getPersonal
app.get(‘/getUser‘, function(req, res) {
res.status(200),
res.json(data)
})
// 配置服务端口
var server = app.listen(4000, function() {
var host = server.address().address
var post = server.address().post
console.log(‘启动成功!!!‘)
})