vue-cli本地mock數(shù)據(jù)

在dev-server.js中添加下面代碼

var apiRoutes = express.Router();
var db=require("../db.json")
var name=db.name

apiRoutes.get('/json',function (req,res) {
  res.json({
    error:0,
    data:name
  });
});

//最后不要忘了 app.use一下
app.use('/api',apiRoutes);

over!

順便提一下
var router=express.Router()

官網(wǎng)上的例子

// invoked for any requests passed to this router
router.use(function(req, res, next) {
  // .. some logic here .. like any other middleware
  next();
});

// will handle any request that ends in /events
// depends on where the router is "use()'d"
router.get('/events', function(req, res, next) {
  // ..
});

...

app.use('/calendar', router);


官當(dāng)文檔

app.use([path,] callback [, callback...])

Mounts the specified middleware function or functions at the specified path: the middleware function is executed when the base of the requested path matches path

掛載特殊的中間件在路徑上,當(dāng)路徑匹配時,執(zhí)行中間件函數(shù)
.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容