简体   繁体   中英

nodejs config on subdirectory

I have a sub-domain and I publish my server.js on this directory. 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). for example:

web.example.com/sr

web is my subdomain and sr is my directory.

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 . Every call to your.domain.com/sr/* will end up in your server and pass /sr/* to it (and not simply /* .

You'll have to code your server to serve your React app. 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):

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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