Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Can';t使用Express/Node服务静态HTML文件_Node.js_Reactjs_Express - Fatal编程技术网

Node.js Can';t使用Express/Node服务静态HTML文件

Node.js Can';t使用Express/Node服务静态HTML文件,node.js,reactjs,express,Node.js,Reactjs,Express,我正在使用带有节点的Express,然后用它构建一个React应用程序,除了一个静态HTML文件外,它是一个页面 我在本地工作,所以当我转到localhost:3000/static file.html时,我可以看到该文件。但这在生产中不起作用 我甚至不能点击任何testconsole.log语句,所以我知道在请求static file.html时没有任何东西被点击。请参见我的app.js文件的以下部分: if (process.env.NODE_ENV === "production") {

我正在使用带有节点的Express,然后用它构建一个React应用程序,除了一个静态HTML文件外,它是一个页面

我在本地工作,所以当我转到
localhost:3000/static file.html
时,我可以看到该文件。但这在生产中不起作用

我甚至不能点击任何test
console.log
语句,所以我知道在请求
static file.html
时没有任何东西被点击。请参见我的
app.js
文件的以下部分:

if (process.env.NODE_ENV === "production") {
  app.get('/static-file.html', function (req, res) {
    console.log('test1');
  });
  app.get('static-file.html', function (req, res) {
    console.log('test2');
  });
  app.get('static-file', function (req, res) {
    console.log('test3');
  });
  app.get('/static-file', function (req, res) {
    console.log('test4');
  });

  app.use(express.static("client/build"));
}
当我转到production-site.com/static-file.html时,我仍然看到索引,这与localhost不同


有人能帮我吗?非常感谢。

最后您必须发送回复。控制台后请执行res.send({})或res.render(static file.html)@MohitTilwani但是控制台中没有显示任何东西表明
console.log()
语句被击中,如果
app.get()
语句正常,我应该在控制台中看到一些东西,不是吗?谢谢您的回复。您的
get()
调用将全部超时。如果您真的想让它们只记录东西,那么您需要为它们添加第三个参数,并调用
next
回调:
app.get('/static file.html',函数(req,res,next){console.log('whatever');next();})
明白了,谢谢。不幸的是,他们中没有一人被击中。在进一步测试之后,
/
似乎总是被击中。也许我做得不对。我只是不知道如何让它工作。你确定process.env.NODE_env在你的环境中等同于生产吗?Do app.get(“*”,函数(req,res){console.log(“ok”);返回res.status(200.json({});});如果没有看到日志或响应,则说明process.env.NODE_env的值有问题,或者请求之前被另一个处理程序捕获
Try something like this:
    const express = require('express');
    const app = express();
    const path = require('path');

    // the __dirname is the current directory from where the script is running
    app.use(express.static(__dirname));
    app.use(express.static(path.join(__dirname, 'build')));
    app.get('*', (req, res) => {
      res.sendFile(path.join(__dirname + '/build/index.html'))
    })

    /* Here build is the production build directory and index.html is the main html page to show */