[英]How to block server.js in node.js
Problem statement问题陈述
I have server.js in which I have a config file我有 server.js,其中有一个配置文件
My server.js我的 server.js
Some imports部分进口
require config.js -> api call to get config /promise call需要 config.js -> api 调用来获取配置 /promise 调用
Server start using value in config服务器开始使用配置中的值
Before I get config, server tries to start and crash as config is empty.在我获得配置之前,服务器尝试启动并崩溃,因为配置为空。
How do I block process to go beyond config.js step如何阻止进程超越 config.js 步骤
在承诺完成之前,您无法启动服务器,因此您实际上拥有非空配置。
I don't know if I understand completely your situation.不知道我是否完全了解你的情况。 But, if you want to start your server after the request you can use the
then
or async / await
to do that.但是,如果你想在请求之后启动你的服务器,你可以使用
then
或async / await
来做到这一点。 Considere this simple snippet:考虑这个简单的片段:
const express = require('express');
const app = express();
const getConfig = require('./config');
const expressConfigs = (app, config) => {
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(config.port, () => {
console.log(`Example app listening on port ${config.port}!`);
});
};
(async () => {
const config = await getConfig();
expressConfigs(app, config);
})();
OBS: config soube export one Promise, you could use node-fetch for that. OBS:config soube 导出一个 Promise,你可以使用 node-fetch。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.