当前位置:主页 > 查看内容

小程序订餐系统——后端开发

发布时间:2021-05-28 00:00| 位朋友查看

简介:这个项目前端逻辑性较强后端处理相对简单数据也没存在数据库中存入了不同的json文件中 app.js // npm install express --save // npm install ejs --save var fs require ( fs ) ; var express require ( express ) ; var bodyParser require ( body-parser……

这个项目前端逻辑性较强,后端处理相对简单,数据也没存在数据库中,存入了不同的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://blog.csdn.net/weixin_45525272/article/details/115531373
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:Linux JSON配置文件操作 下一篇:没有了

推荐图文


随机推荐