如何将自定义路由移出jsonServer中的server.js之外

时间:2019-07-19 01:03:39

标签: javascript node.js json

server.js中,我正在使用json-server。我要搬走

server.get('/api/1.0/searchDoc', (req, res) => {
    const searchType = req.query['searchType'];
    const searchValue = req.query['searchValue'];
    res.status(200).jsonp({searchType, searchValue});
});

route.js,然后导入server.js。我该如何实现?

完整代码

// json server
const jsonServer = require('json-server');
// create server
const server = jsonServer.create();

// data as router
const allDocData = require('../response.json');
const searchDocData = require('../searchResponse.json');
const dataObj = {
    'mockResponse': allDocData,
    'searchDocData': searchDocData
};

const router = jsonServer.router(dataObj);

// mid-ware
const middlewares = jsonServer.defaults();

// package.json uses port 4000
const port = 4000;

// live before router
// e.g. http://localhost:4000/api/1.0/searchDoc/?searchType='customerId'&searchValue='1234'
server.get('/api/1.0/searchDoc', (req, res) => {
    const searchType = req.query['searchType'];
    const searchValue = req.query['searchValue'];
    res.status(200).jsonp({searchType, searchValue});
});

// use mid-ware
server.use(middlewares);
// use data
server.use(router);

// user body parser
server.use(jsonServer.bodyParser);
// use mid-ware
server.use(middlewares);

// use router
server.use(router);

// listen
server.listen(port);

2 个答案:

答案 0 :(得分:0)

您的意思是这样吗? www.js

const JSONServer = require('jsonserver')

function build() {
 const server = JSONServer.create()

 server.get('/api/1.0/searchDoc', (req, res) => {
    const searchType = req.query['searchType'];
    const searchValue = req.query['searchValue'];
    res.status(200).jsonp({searchType, searchValue});
 });

 return server
}

module.exports = build

例如,在另一个文件index.js

const build = require('./www.js')
const server = build()
// use mid-ware
server.use(middlewares);
// use data
server.use(router);

// user body parser
server.use(jsonServer.bodyParser);
// use mid-ware
server.use(middlewares);

// use router
server.use(router);

// listen
server.listen(port);

只要退还就可以。

答案 1 :(得分:0)

有很多方法可以实现-另一种方法是将server实例注入到route.js模块中,这将使您的应用程序的路由关注点提取为:

route.js

/* 
Define default module function that configures apps routing for the specified 
server instance
*/
module.exports = function(server) {

    server.get('/api/1.0/searchDoc', (req, res) => {
        const searchType = req.query['searchType'];
        const searchValue = req.query['searchValue'];
        res.status(200).jsonp({searchType, searchValue});
    });
}

server.js

const jsonServer = require('json-server');

/* 
Import default module function from route.js module. We'll assing this
function to the configureRoutes variable and call it in the server.js when
needed. This import assumes route.js exists in the same directory of this
source file 
*/
const configureRoutes = require('./route.js');

const server = jsonServer.create();
const allDocData = require('../response.json');
const searchDocData = require('../searchResponse.json');
const dataObj = {
    'mockResponse': allDocData,
    'searchDocData': searchDocData
};

const router = jsonServer.router(dataObj);
const middlewares = jsonServer.defaults();

const port = 4000;

/*
Call configureRoutes() to configure routes on your server instance via the
new route.js module
*/
configureRoutes(server);

server.use(middlewares);
server.use(router);
server.use(jsonServer.bodyParser);
server.use(middlewares);
server.use(router);

server.listen(port);