Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
672 views
in Technique[技术] by (71.8m points)

javascript - create-react-app显示错误“您需要启用JavaScript才能运行此应用程序。”(create-react-app shows an error of “You need to enable JavaScript to run this app.”)

I use express for my API.(我将express用于我的API。)

I have a folder named app and another folder named server.(我有一个名为app的文件夹和另一个名为server的文件夹。) app is the client app, using create-react-app as boilerplate, while server is express.js app for the API.(app是客户端应用程序,使用create-react-app作为样板,而服务器是API的express.js应用程序。)

in the app.js file of the server, I wrote(在服务器的app.js文件中,我写道)

app.get("*", function (req, res) {
    res.sendFile(path.resolve(__dirname, '../app/build/index.html'));
})

But then when I call any API endpoint, I get(但是当我调用任何API端点时,)

You need to enable JavaScript to run this app.(您需要启用JavaScript才能运行此应用。)

in the response.(在回应中。)

I'm confused;(我糊涂了;) what's wrong?(怎么了?)   ask by Jenny Mok translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the build directory you have more files that just index.html.(在构建目录中,您仅拥有index.html的更多文件。)

You also have build/js/main.buildNumber.js and build/css/main.buildNumber.css.(您还拥有build / js / main.buildNumber.js和build / css / main.buildNumber.css。) So when your frontend makes a request to https://yourdomain.com/css/main.buildNumber.js , it returns index.html not main.js.(因此,当您的前端向https://yourdomain.com/css/main.buildNumber.js发出请求时,它将返回index.html 而不是 main.js。)

You serve the contents of the build folder statically with express.static(您可以通过express.static静态地提供构建文件夹的内容)

    app.use('/', express.static(__dirname + '/'));

Or you can look into the "serve" node module to host your app.(或者,您可以查看“服务”节点模块来托管您的应用程序。)

This will work nicely with react-router.(这将与react-router一起很好地工作。) npm i -g serve then cd build then serve . --single -p 5000(npm i -g serve然后cd build然后serve . --single -p 5000) serve . --single -p 5000 .(serve . --single -p 5000 。) This will serve your app on port 5000.(这将在端口5000上为您的应用程序提供服务。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...