如题,使用node.js为前端搭建一个简易型的后台服务

var http=require('http');
// 创建服务器
http.createServer(function(req,res){
  var postData="";
  // 允许跨域访问
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader("Access-Control-Allow-Origin", "http://localhost:4200"); 
  req.setEncoding('utf8');
  res.writeHead(200, {'Content-Type': 'application/json'});
  req.on('data',function(chunk){
         postData+=chunk;
  });
  req.on('end',function(){
       res.end(postData);
     });
  }).listen(3000);
console.log("服务启动。。。")

值得注意的是,跨域问题需要在回应函数中添加 res.setHeader('Access-Control-Allow-Origin', '*');以允许跨域访问该服务器。

 

相关文章:

  • 2019-09-11
  • 2021-11-04
  • 2021-10-19
  • 2022-01-08
  • 2022-01-28
  • 2021-12-23
  • 2022-01-08
猜你喜欢
  • 2021-09-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-08
  • 2022-12-23
  • 2021-05-22
相关资源
相似解决方案