简体   繁体   English

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

[英]How move custom route outside of server.js in jsonServer

In server.js , I am using json-server . server.js ,我正在使用json-server I want to move 我要搬家

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

to route.js then import in server.js . route.js然后导入server.js How do I achieve it? 我该如何实现?

Full code 完整代码

// 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);

Do you mean like this? 你是这个意思吗 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

And on another file index.js for example 以另一个文件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);

you can if you just return it. 您可以将其退回。

There are a number of ways this could be achieved - another approach would be to inject the server instance into the route.js module which would enable your app's routing concern to be extracted as: 有许多方法可以实现-另一种方法是将server实例注入route.js模块,这将使您的应用程序的路由关注点提取为:

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 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM