调用前使用npm添加express模块
var express = require('express');
var app = express();
var urlencodedParser = bodyParser.urlencoded({
extended: false
})
app.use(express.static('public'));
//allow custom header and CORS
app.all('*', function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
if(req.method == 'OPTIONS') {
res.send(200);
/让options请求快速返回/
} else {
next();
}
});