简体   繁体   English

在node.js上发出HTTP请求是否正常

[英]Is it a normal to make http request on node.js

I'm using SSR with react and i18next. 我正在将SSR与react和i18next一起使用。 I don't have a lot experience with Node.js, so the question is: Is it a normal way to make an external http request in a start of a server script execution and pass all the left server script code into the then function of a returned Promise instance as a later continuation part of the script. 我对Node.js经验不足,所以问题是:这是在服务器脚本执行开始时发出外部http请求并将所有剩余的服务器脚本代码传递给then函数的一种正常方法吗?返回的Promise实例,作为脚本的后续部分。

Server code which is bundled and then is started 捆绑并启动的服务器代码

server.js

import express from 'express';
import promiseRequest from 'request-promise';
import i18next from "i18next";
import middleware from "i18next-express-middleware";
import render from './render';

const app = express();

promiseRequest.get('https://api/localization')
  .then(data => {
    i18next.use(middleware.LanguageDetector);
    i18next.init();

    app.use(middleware.handle(i18next));

    app.listen(3000, () =>
      console.log('App is running')
    );

    return app;
  });

The reason I go this way is because I need to init i18n based on a response from a server. 我采用这种方式的原因是因为我需要基于服务器的响应来初始化i18n。

Why do you need to make an api request to figure out your localization needs? 为什么您需要发出api请求来确定您的本地化需求? Wouldn't that be a part of the incoming request to your static server (gimme the website in english, french, etc)? 那不是对您的静态服务器的传入请求的一部分吗(给网站提供英语,法语等)? I'm unclear about your use case. 我不清楚您的用例。 Are you trying to have several instances of your static server to serve different languages? 您是否要让静态服务器的多个实例提供不同的语言服务?

A common model I've seen is to have multiple static files pre-generated, per localization, and then serve them up based on the request, ex: have a route that returns the english version when the user makes a request to http://yourwebsite.com/en , the french one at http://yourwebsite.com/fr or http://yourwebsite.fr , etc. 我见过的一个常见模型是,每个本地化都会预先生成多个静态文件,然后根据请求将它们提供服务,例如:具有一条当用户向http:/请求时返回英文版本的路由/yourwebsite.com/en ,法语网址http://yourwebsite.com/frhttp://yourwebsite.fr等。

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

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