简体   繁体   English

在node / express App的静态目录中呈现index.html

[英]rendering index.html in static directory in node/ express App

I am having issue rendering index.html in my react/node/express app in production environment. 我在生产环境中的react / node / express应用中渲染index.html时遇到问题。 My app file structure is: 我的应用程序文件结构为:

 -project --app ---dist ----index.html ----main.js ----style.css --server ---server.js 

With my server/server.js looking like this: 与我的server/server.js看起来像这样:

 var express = require('express'); var bodyParser = require('body-parser'); var mongoose = require('mongoose'); var path = require('path'); require('dotenv').config(); require('dotenv').load(); var app = express(); var router = express.Router(); var cons = require('consolidate'); // view engine setup app.engine('html', cons.swig) app.get('/',function(req,res){ res.sendFile(path.join(__dirname,"../app/dist/index.html")); }); // controllers and mailers var emailController = require('./controllers/emailController'); var campaignController = require('./controllers/campaignController'); // Express request pipeline app.use(express.static(path.join(__dirname, "../app/dist"))); app.use(bodyParser.json()); app.use('/api/email', emailController); //any request in the form of /api will be handled by emailController app.use('/api/campaign', campaignController); //any request in the form of /api will be handled by emailController var theport = process.env.PORT || 7777; app.listen(theport, function(){ console.log("started listening on port " + theport); }); 

Here is my package.json 这是我的package.json

 { "name": "project_s", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \\"Error: no test specified\\" && exit 1", "start": "node server/server.js" }, "engines": { "node": "5.11.1" }, "author": "Oat Wongsajjathiti (oatlikeoatmeal.com)", "license": "ISC", "dependencies": { "body-parser": "^1.15.2", "boron": "^0.2.3", "bower": "^1.7.9", "browserify": "^13.1.0", "consolidate": "^0.14.1", "dotenv": "^2.0.0", "es6-promise": "^4.0.3", "express": "^4.14.0", "fs": "0.0.1-security", "guid": "0.0.12", "gulp": "^3.9.1", "jquery": "^3.1.1", "json2csv": "^3.7.0", "moment": "^2.15.1", "mongodb": "^2.2.10", "mongoose": "^4.6.1", "nodemailer": "^2.6.4", "nodemailer-ses-transport": "^1.4.0", "nodemon": "^1.10.2", "rc-time-picker": "^2.0.2", "react": "^15.3.2", "react-date-picker": "^5.3.28", "react-datepicker": "^0.29.0", "react-dom": "^15.3.2", "react-file-input": "^0.2.5", "react-timepicker": "^1.1.3", "reactify": "^1.1.1", "sendgrid": "^4.4.0", "sendgrid-rest": "^2.2.2", "ses": "0.0.1", "setimmediate": "^1.0.5", "swig": "^1.4.2", "underscore": "^1.8.3", "vinyl-source-stream": "^1.1.0" } } 

When running on local server its fine, but when I deploy to heroku, I get the following error: 在本地服务器上运行时很好,但是当我部署到heroku时,出现以下错误:

Error: ENOENT: no such file or directory, stat '/app/app/dist/index.html' 2016-10-04T20:01:54.987024+00:00 app[web.1]: at Error (native) 2016-10-04T20:02:01.486373+00:00 heroku[router]: at=info method=GET path="/" host=tcb-project-s.herokuapp.com request_id=58797fda-0b12-4046-9390-2088d4b53b2f fwd="207.102.85.144" dyno=web.1 connect=1ms service=5ms status=404 bytes=208

I think I am doing something wrong with the routing, since my file organization is quite different from the recommended express structure, with views in its own file outside dist folder. 我认为路由做错了,因为我的文件组织与建议的express结构完全不同,视图位于dist文件夹之外的自己文件中。

It looks like the paths you're using are messed up: 您正在使用的路径似乎混乱了:

app.use(express.static(path.join(__dirname, "../app/dist")));

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

Try changing these values to: 尝试将这些值更改为:

path.join(__dirname, "./dist")

path.join(__dirname,"./dist/index.html")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 表示不提供整个静态目录,仅提供index.html - Express not serving entire static directory just index.html 带有静态 HTML 的 Node + Express。 如何将所有请求路由到 index.html? - Node + Express with static HTML. How to route all requests to index.html? 在node express中创建一个静态文件夹,该文件夹承载index.html和其他tsv文件 - Creating a static folder in node express that hosts index.html and other tsv files Express static 适用于 index.html 但不适用于其他 static 页面 - Express static works for index.html but not other static pages express.static仅加载到index.html - express.static only loading to index.html 带有 express 的节点服务器 - 使用 res.send({}) 将 dinamyc html 注入 index.html - Node server with express - Inject dinamyc html into index.html using res.send({}) Nodejs + Express始终返回index.html - Nodejs + Express always returning index.html Express呈现index.html,但没有其他页面 - Express renders index.html but no other page webpack + express + angular 4错误“错误:ENOENT:没有这样的文件或目录,统计信息'C:\\ public \\ index.html' - webpack + express + angular 4 error "Error: ENOENT: no such file or directory, stat 'C:\public\index.html' 使用 API 中的 HTML 而不是 index.html 中的静态 HTML 来为我的 React 应用程序补充水分 - Hydrate my React app with HTML from the API instead of static HTML from index.html
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM