Express中 res.json 和res.end 及res.send()

本文详细解析了如何在优快云上发布一篇高质量的技术博客,包括标题、标签和内容的优化策略,旨在帮助作者提高文章的可见性和吸引力。
var fs = require('fs'); var express = require("express"); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.json()); app.get('/api/food/index',function (req,res){ console.log(req.query); fs.readFile('index.json','utf-8',function(err,data) {if(err){ console.log(err); }else{ res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'}); res.end(data); } }); }); app.get('/api/food/list',function (req,res){ console.log(req.query); fs.readFile('list.json','utf-8',function(err,data){ if(err){ console.log(err); }else{ res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'}); res.end(data); } }); }); app.get('/api/food/orderlist',function (req,res){ console.log(req.query); var filename = 'orderlist-0.json'; if(req.query.last_id === "10"){ filename = 'orderlist-10.json'; }else if(req.query.last_id === "20"){ filename = 'orderlist-20.json'; } fs.readFile(filename,'utf-8',function(err,data){ if(err){ console.log(err); }else{ res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'}); res.end(data); // res.end(JSON.stringify(JSON.parse(data))); } }); }); app.post("/api/food/order",function(req,res){ res.json({error:0,order_id:3}) }); app.get("/api/food/order",function(req,res){ fs.readFile('order.json','utf-8',function(err,data){ if(err){ console.log(err); }else{ res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'}); res.end(data); // res.end(JSON.stringify(JSON.parse(data)[0])); } }); }); app.post("/api/food/pay",function(req,res){ res.json({error:0,order_id:3}) }); app.get('/api/food/record',function(req,res){ console.log(req.query); fs.readFile('record.json','utf-8',function(err,data){ if(err){ console.log(err); }else{ res.writeHead(200,{'Content-Type':'application/json;charset=utf-8'}); res.end(data); } }); }); app.listen(8081) 以上ef.js文件module.exports = { baseUrl: 'http://localhost:8081/api/food' } 这是封装文件
最新发布
06-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值