我正在使用NodeJS并在我的项目中表达,在本地一切都很好, 我的问题仅在生产中出现: 服务器没有显示主页(任何其他途径都会给我一个未找到的404),而是向我显示了应用程序目录的内容,因此我认为apache中的反向代理正在完成他的工作。 这是我的代码:
/etc/apache2/sites-available/ririgram.richardmeuret.dev.conf:
<VirtualHost *:80>
ServerAdmin webmaster@richardmeuret.dev
ServerName ririgram.richardmeuret.dev
ServerAlias www.ririgram.richardmeuret.dev
DocumentRoot /var/www/html/ririgram
# LogFiles
ErrorLog /var/www/html/ririgram/logs/error.log
CustomLog /var/www/html/ririgram/logs/access/log combined
ProxyRequests Off
ProxyPass / http://127.0.0.1:3002/
ProxyPassReverse / http://127.0.0.1:3002/
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from 127.0.0.1
</Proxy>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.ririgram.richardmeuret.dev [OR]
RewriteCond %{SERVER_NAME} =ririgram.richardmeuret.dev
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
app.js:
/* NodeJS code modules */
const path = require('path');
/* npm modules */
const express = require('express');
// hbs module so we can use partial templates
const hbs = require('hbs');
// call mongoose.js, it will launch the file, connect to db and create database if doesn't exist
require('./db/mongoose');
// importing routes
// here node doesn't understand them, just to have a ref
const userRouter = require('./routers/user');
const gridRouter = require('./routers/grid');
const optionsRouter = require('./routers/options');
const feedbackRouter = require('./routers/feedback');
const app = express();
// for dev in vagrant
const hostname = '127.0.0.1';
// used port
const port = 3002;
/* Define paths for express config */
// build the public path from absolute path
const publicDirectoryPath = path.join(__dirname, '../public');
// views directory
const viewsPath = path.join(__dirname, '../templates/views');
// partials templates location
const partialPath = path.join(__dirname, '../templates/partials');
/* Setup handlebars engine and views location */
// Tell express we're gonna use hbs as a template engine
app.set('view engine', 'hbs');
// Tell express we have moved the views directory
app.set('views', viewsPath);
// Tell hbs we're gonna use some partial templates
hbs.registerPartials(partialPath);
// Tell hbs that we need to use ifEquals to compare 2 values in hbs files
hbs.registerHelper('ifEquals', function(arg1, arg2, options) {
return (arg1 == arg2) ? options.fn(this) : options.inverse(this);
});
/* Setup static directory to serve */
// with this, we don't need a route for '', il will send directly to public path (so the index.html wich is inside)
// no need either of routes for '/help' and '/about' as I've created public/about/index.html and public/help/index.html
// Ici on voit bien que ce ne sera utiliser que par des pages statiques et non dynamiques ;)
// it will also give access to js and css from views ! mandatory !
app.use(express.static(publicDirectoryPath))
// Tell express to parse json when we receice some (NEEDED for POST requests !)
app.use(express.json());
// home route
app.get('', (req, res) => {
res.render('index');
});
// register our routes in express
app.use(userRouter);
app.use(gridRouter);
app.use(optionsRouter);
app.use(feedbackRouter);
// 404 Error page, !! this must be the very last route
app.get('*', (req, res) => {
res.status(404).render('404', {
title: 'ririgram',
author: 'Richard Meuret',
message404: 'Page non trouvée !'
});
});
// Launch server
app.listen(port, hostname, () => {
console.log(`Server is up at ${hostname}:${port}`);
});
感谢您的帮助!
答案 0 :(得分:0)
更改此
app.get('', (req, res) => {
res.render('index');
});
对此
app.get('/', (req, res) => {
res.render('index');
});
答案 1 :(得分:0)
我为此做了很多尝试,包括删除我的projetct和apache vhost。 但是现在一切正常,我真的不知道出什么问题了... 问题是否可能是由于项目文件夹的权限不足引起的?