NodeJS_0003:nodejs 设置允许跨域请求

1,


index.js文件中:

var
http = require('http'); var express = require('express'); var router = express.Router(); var cp = require("child_process"); var nodeCmd = require('node-cmd'); var fs = require("fs"); // 允许跨域请求 router.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-Width"); 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"); if (req.method == "OPTIONS") { return res.end(); }; next(); }); // 生成机器码并写入文件 // const { machineId, machineIdSync } = require('node-machine-id'); // var macId = machineIdSync(); // fs.writeFile('../机器码.txt', '' + macId, function(err) { // if (err) { // return console.error(err); // } // console.log("数据写入成功!"); // }); // 启动本地程序 // nodeCmd.run("start D:/OPCloud/OPS/worker/OPS.exe"); router.get('/', function(req, res, next) { res.render('index', { title: 'XXXX' }); }); router.post('/getMacId', function(req, res) { res.json({ "data": macId }); }); // router.post('/down', function(req, res) { // cp.execFile("D:/OPCloud/OPS/language/down2.bat", null, { cwd: '' }, function(error, stdout, stderr) { // if (error !== null) {} else console.log("成功") // }); // res.json({ "data": "downok" }); // }); module.exports = router;

 

posted @ 2020-07-08 09:53  琥珀君  阅读(683)  评论(0编辑  收藏  举报