如何在同一节点服务器上服务React App和API



我正在尝试在同一服务器上使用我的构建的React App API。

我尝试了代码:

const express = require('express');
const bodyParser = require('body-parser')
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/ping', function (req, res) {
    return res.send('pong');
});
app.get('/', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(process.env.PORT || 8080);

但是,无论我在浏览器中使用什么URL,它都能提供"构建"文件夹(我的React App(中的index.html。localhost:8080/ping为index.html和non'pong'。

使用静态文件和应用程序。使用express.js

获得冲突

将您的index.html文件重命名为其他。就是那个简单

app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
    if(req.session){
        console.log(req.session);
    }
    console.log('ok');
    res.sendfile(new_index_file);
});

最新更新