mock數據

做爲前端常常須要模擬後臺數據,咱們稱之爲mock。一般的方式爲本身搭建一個服務器,返回咱們想要的數據。 在這裏咱們使用node.js來實現html

http://www.cnblogs.com/bsn-huang/p/3916951.html前端

 

數據來源data.json:node

{

"seller": { "name":"hello", "type":"true" },

"goods": [ { "deliveryTime": 30, "score": 5 }, { "deliveryTime": "", "score": 4 } ],

"ratings": [ { "username": "3******c", "rateTime": 1469281964000 }, { "username": "2******3", "rateTime": 1469271264000 } ]

}

 

index.js:express

var express = require('express'); 
var app = express(); 
var router = express.Router(); 

var appData = require('./data.json'); 
var seller = appData.seller; 
var goods = appData.goods; 
var ratings = appData.ratings; 

router.get('/seller', function (req, res) {
    res.json({ 
        errno: 0, 
        data: seller }); 
    }); 
router.get('/goods', function (req, res) { 
    res.json({ 
        errno: 0, 
        data: goods }); 
    }); 
router.get('/ratings', function (req, res) { 
    res.json({ 
        errno: 0, 
        data: ratings }); 
    }); 

app.use(router); 
app.listen(3000); 
console.log("time")
相關文章
相關標籤/搜索