美文网首页
手写的服务(基于node)(个人 整理 不一定对)

手写的服务(基于node)(个人 整理 不一定对)

作者: 五道杠_9225 | 来源:发表于2018-10-22 22:03 被阅读25次

let http = require('http');

let fs = require('fs');

let url = require('url');


http.createServer(function (req, res){

(请求头信息)

res.setHeader("Access-Control-Allow-Origin", "*"); res.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With"); res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS"); res.setHeader("X-Powered-By", ' 3.2.1')

//获取热门图书

  if(pathname==='/hot'){

    fs.readFile('./book.json','utf8',function (err,data) {

      res.setHeader('content-type','application/json;charset=utf-8');

      data =  JSON.parse(data).reverse().slice(0,6)

      return  res.end(JSON.stringify(data));

    });

  }

(对应的API)

export let getHot = () => { return axios.get("/hot")}

}).listen(3000);

相关文章

网友评论

      本文标题:手写的服务(基于node)(个人 整理 不一定对)

      本文链接:https://www.haomeiwen.com/subject/fcunzftx.html