1. npm init creates package.json
2. Create the app.js file and configure the app.js file
(1)var express = require(“express”);
(2) var app = express () ;
(3) Set up cross-domain access
1)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();
})
(4) Simulate the data transmitted to the foreground
1 questions var questions = [{
data:213,
num:444,
age:12
},{
data:456,
num:678,
age:13
}]
(5) Interface configuration
app.get('/123',function(req,res){
res.status(200);
res.json(questions);
})
(6) Configure the service port
var server = app.listen(3000,function(){
console.log('Service started successfully');
})