简体   繁体   English

子目录上的nodejs配置

[英]nodejs config on subdirectory

I have a sub-domain and I publish my server.js on this directory. 我有一个子域,并在此目录上发布了server.js everything work fine. 一切正常。 but I want to running my server.js inside a directory(because I want to run my react.js project on sub directory). 但我想在目录中运行我的server.js (因为我想在子目录中运行我的react.js项目)。 for example: 例如:

web.example.com/sr web.example.com/sr

web is my subdomain and sr is my directory. web是我的子域, sr是我的目录。

but my routes not worked at all: 但我的路线根本不起作用:

web.example.com/sr/user/1

I got this error message: 我收到此错误消息:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Error</title>
    </head>
    <body>
        <pre>Cannot POST /sr/user/1</pre>
    </body>
</html>

Should I make any changes or is there any config to do this? 我应该进行任何更改还是要进行任何配置?

You can't do deployment-level configs to serve your react app to the subdir /sr . 您无法执行部署级配置来将您的react应用程序提供给subdir /sr Every call to your.domain.com/sr/* will end up in your server and pass /sr/* to it (and not simply /* . 每次对your.domain.com/sr/*调用都将在您的服务器中结束,并将/sr/*传递给它(而不是简单地/*

You'll have to code your server to serve your React app. 您必须对服务器进行编码才能提供React应用程序。 If you bundle your react app to index.html and bundle.js , you'll have to write something like this (if you're using express, which you probably do): 如果将react应用程序捆绑到index.htmlbundle.js ,则必须编写类似以下内容(如果使用的是express,则可能会这样做):

app.get(/\/sr\/\.js/, (req, res) => res.sendFile(`${__dirname}/bundle.js`));
app.get(/\/sr\/*/, (req, res) => res.sendFile(`${__dirname}/index.html`));

If you're using react-router, you'll have to set it up too so as to take into account the leading /sr in your URL. 如果您使用的是react-router,则还必须对其进行设置,以考虑URL中的前导/sr

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

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