这个项目前端逻辑性较强,后端处理相对简单,数据也没存在数据库中,存入了不同的json文件中
// npm install express --save
// npm install ejs --save
var fs = require('fs');
var express = require("express");
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.json());
//get请求首页信息
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);
res.end(data);
}
});
});
//get请求菜单列表
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);
res.end(data);
}
});
});
//get请求订单列表
app.get('/api/food/orderlist',function (req,res) {
console.log(req.query);
var filename = 'orderlist-0.json';
if (req.query.last_id === "10") {
// 10 : 11~20
filename = 'orderlist-10.json';
}else if (req.query.last_id === "20") {
// 20: 21~30
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)));
}
});
});
//订单请求post
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]));
// 因为搭建服务器比较麻烦,这里采用模拟数据
res.end(JSON.stringify(JSON.parse(data)[0])); // 表示未取餐
// res.end(JSON.stringify(JSON.parse(data)[2])); 表示已取餐
}
});
});
//支付post请求
app.post("/api/food/pay",function(req,res){
res.json({error:0,order_id:3})
});
//get请求消费记录
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);
res.end(data);
}
});
});
app.listen(8081);
/*
//模板引擎
app.set("view engine","ejs");
app.get("/",function(req,res){
res.render("form");
});
/*
//bodyParser API
app.use(bodyParser.urlencoded({ extended: false }));
app.post("/",function(req,res){
console.log(req.body);
});
*/
这里的数据较多,且示例一两个
index.json
[
{
"imgUrls": [{
"id": 1,
"src": "../../images/banner_1.png"
},
{
"id": 2,
"src": "../../images/banner_2.png"
},
{
"id": 3,
"src": "../../images/banner_3.png"
}],
"image_ad":"../../images/image_ad.png",
"image_bottom":[{
"id": 1,
"src":"../../images/bottom_1.png"
},
{
"id": 2,
"src":"../../images/bottom_2.png"
},
{
"id": 3,
"src": "../../images/bottom_3.png"
},
{
"id": 4,
"src":"../../images/bottom_1.png"
}]
}
]
前言 github做为一个庞大的开源宝库, 作为程序猿我相信你不会陌生的,里面有着众...
本文讲述使用JSP实现用户登录,包括用户登录、注册和退出功能等。 1.系统用例图 ...
经常有朋友问起,如何在IM即时通讯中实现发送图片、视频、语音和表情? 为此,小...
数据库基本概念 数据库是一个以某种有组织的方式存储的数据集合。数据库(databas...
本文介绍如何终止所有用户会话、特定的用户回话。 终止所有用户会话 下面的shell...
图片来自 Pexels 有些优化策略需要你有一定的 SQL 实践才能体会其中的道理,当然...
使用React Native从零开始开发一款移动应用是一件很惬意的事情,但对于一些已经...
前言 本文主要给大家介绍的是关于读取数据库数据整合成3D饼图并在jsp中显示的相...
本篇博客主要讲解以下问题: Git 常用命令 创建新仓库 检出仓库 添加与提交 推送...
据外媒报道,微软周一表示,水下数据中心是一个发展方向,在其研究人员将其实验...