标签:set add app 一个 utf-8 fun server cat func
var express = require(‘./node_modules/express‘)
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(‘启动成功!!!‘)
})
标签:set add app 一个 utf-8 fun server cat func
原文地址:https://www.cnblogs.com/guokt/p/12863084.html