Javascript 刷新时未找到节点服务器索引文件(f5)
我尝试设置一个express服务器来承载我的react单页应用程序。当我运行脚本并打开Javascript 刷新时未找到节点服务器索引文件(f5),javascript,node.js,express,http,https,Javascript,Node.js,Express,Http,Https,我尝试设置一个express服务器来承载我的react单页应用程序。当我运行脚本并打开http://localhost:8080在我的浏览器上,index.html文件已加载,我可以在应用程序中导航 但是当我刷新页面时,url包含一些路径(例如http://localhost/items而不是http://localhost/)我收到以下错误: 错误:enoint:没有这样的文件或目录,stat'/index.html' 如果我删除url的路径http://localhost/然后刷新页面,我再
http://localhost:8080
在我的浏览器上,index.html文件已加载,我可以在应用程序中导航
但是当我刷新页面时,url包含一些路径(例如http://localhost/items
而不是http://localhost/
)我收到以下错误:
错误:enoint:没有这样的文件或目录,stat'/index.html'
如果我删除url的路径http://localhost/
然后刷新页面,我再次获得index.html,应用程序再次运行
import http from 'http';
import https from 'https';
import express from 'express';
import path from 'path';
import cors from 'cors';
var env = {
proc: 'http',
port: 8080,
host: 'localhost',
};
app.use(cors());
app.options('*',cors());
app.use(function allowCrossDomain(req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, PUT, POST, DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.use(express.static('./build/public'));
app.get('*', function(req, res) {
console.log('test');
//These don't work
return res.sendFile(__dirname + 'index.html');
//return res.sendFile(path.resolve(__dirname + 'index.html', '../'))
//return res.sendFile('public/index.html' , { root : __dirname});
//return res.sendFile('index.html');
});
var server = (env.proc != 'https')
? http.createServer(app)
: https.createServer(null, app);
server.listen(env.port, function() {
console.log('' + env.proc + '://' + env.host + ':' + env.port + ' listening...');
});
我想在app.get('*',function(req,res){…}上使用*
代码>服务器将在每次get调用时使用作为第二个参数给出的响应进行响应(该参数应将index.html文件作为响应发送)。因此,在这两个http://localhost:8080
和http://localhost/items
服务器应使用索引文件进行响应。但这不起作用
我已经试着替换这个回答
return res.sendFile(__dirname + 'index.html');
我的答复如下:
return res.sendFile(path.resolve(__dirname + 'index.html', '../'))
但我也犯了同样的错误。我也试过这个
return res.sendFile('public/index.html' , { root : __dirname});
但是我得到了这个错误
错误:enoint:没有这样的文件或目录,stat'/public/index.html'
这是我的webpack.server.js
var webpack = require('webpack');
var path = require('path');
var fs = require('fs');
var nodeModules = {};
fs.readdirSync('node_modules')
.filter((x) => {
return ['.bin'].indexOf(x) === -1;
})
.forEach((mod) => {
nodeModules[mod] = 'commonjs ' + mod;
});
module.exports = {
entry: ['./src/index.js'],
target: 'node',
mode: 'development',
output: {
filename: 'server.js',
path: path.join(__dirname, '../', 'build'),
},
externals: nodeModules,
plugins: [
new webpack.IgnorePlugin(/\.(css|less)$/),
new webpack.BannerPlugin({
banner: 'require("source-map-support").install();',
entryOnly: false,
raw: true,
}),
],
devtool: 'sourcemap'
};
我的npm脚本
"scripts": {
"build:server": "webpack --config config/webpack.server.js --watch",
"build:client": "webpack --config config/webpack.client.js --watch",
"start": "nodemon build/server.js"
},
我已经查阅了很多有相同问题的帖子,但是这些对我来说都不起作用
有人能帮我做这个吗