這個專案前端邏輯性較強,后端處理相對簡單,資料也沒存在資料庫中,存入了不同的json檔案中
app.js
// 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);
});
*/
json
這里的資料較多,且示例一兩個
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"
}]
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/274475.html
標籤:其他
下一篇:Linux JSON組態檔操作
