如何将json-server的选项设置为模块?

时间:2017-08-01 22:10:14

标签: json-server

假设我们有以下命令行来运行json-server(https://github.com/typicode/json-server):

public partial class testForm : Form { private void testForm_Sometrigger(object sender, EventArgs e) { //some delay. In the actual program, this is thanks to a background worker working. Thread.Sleep(5000); //manually change focus to another process/window before this this.Hide(); //I've been adding a wait here, since in practice the form won't be called again right away. Thread.Sleep(3000); //show form again. this.Show(); // this will be true even if the form isn't actually visible Console.WriteLine("is visible? "+this.Visible.ToString()); } }

如果我们将json-server作为模块运行,我们如何设置这些选项?我可以看到定义了db.json并定义了端口。但目前尚不清楚其他选项如何定义。

{{1}}

1 个答案:

答案 0 :(得分:2)

我找到了如何设置延迟。这需要安装connect-pause包,它也用在json-server代码(https://www.npmjs.com/package/connect-pause)中:

npm install connect-pause

然后在server.js文件中,我添加了以下一个require('connect-pause')并在json服务器应用程序中使用它。这是我的代码:

const fs = require('fs');
const pause = require('connect-pause');
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('db.json');
const middlewares = jsonServer.defaults();

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

//
// Define custom routes (routes.json)
//
var routes = JSON.parse(fs.readFileSync('routes.json'));
server.use(jsonServer.rewriter(routes));

...

server.use(pause(1000));
server.use(router);
server.listen(8008, () => {
  console.log('JSON Server is running');
});

设置任何其他选项变化很大,但我主要需要知道如何设置延迟。