var express = require('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("X-Powered-By", ' 3.2.1');
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
// 数据
var questions = [];
function createRandomItem(id) {
var heroes = ['Amg', 'xiaowu', 'lMy', 'tesu', 'yuqu'];
return {
id: id,
name: heroes[Math.floor(Math.random() * 7)],
age: Math.floor(Math.random() * 40),
saved: Math.floor(Math.random() * 600)
};
}
for (var i = 0; i < 2; i++) {
questions.push(createRandomItem(i));
}
//写个接⼝123
app.get('/123', function(req, res) {
res.status(200),
res.json(questions)
});
app.get('/999', function(req, res) {
res.status(200),
res.json({
data: [
{
id: 1,
num: 600,
type: '1'
}, {
id: 2,
num:80,
type: '3'
}
]
})
});
//配置服务端⼝
const server = app.listen(3020, function() {
var host = server.address().address
var port = server.address().port
console.log('Example app listening at http://%s:%s', host, port)
})