node实现http接口请求(解决跨域)
var express = require('express'); var app = express(); var bodyParser = require('body-parser') app.post('/highway', bodyParser.json(), function (req, res) { var json = { } res.setHeader("Access-Control-Allow-Origin","*") res.json(json) }) app.get('/highway', bodyParser.json(), function (req, res) { var json = { } res.setHeader("Access-Control-Allow-Origin","*") res.json(json) }) //运行接口 app.listen('9000', function () { console.log('port start') })
cmd窗口打开,执行:node server.js
浏览器访问:
http://127.0.0.1:9100/highway