首页 > 解决方案 > Nodejs express 显示服务器内容而不是渲染页面

问题描述

我在我的项目中使用 NodeJS 和 express,本地一切都很好,我的问题只出现在生产中:而不是渲染主页(任何其他路由都会给我一个未找到的 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>

应用程序.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}`);
});

感谢您的帮助 !

标签: node.jsexpress

解决方案


改变这个

 app.get('', (req, res) => {
    res.render('index');
});

对此

app.get('/', (req, res) => {
    res.render('index');
});

推荐阅读