如何在Express.js与Nginx中获取views文件夹下所有页面?
Hey there! Let's break down your problem step by step to get this sorted out.
First, Fix the /event 404 Error
The 404 for /event is almost certainly an issue with your Express.js routing, not Nginx (since Nginx is just forwarding the request to your Node server). Here's what to check:
- You need to explicitly define a route in Express for
/eventthat renders the corresponding view file. For example, if your view isviews/event.ejs(or whatever template engine you're using), your Express code should have something like this:const express = require('express'); const app = express(); app.set('view engine', 'ejs'); // Replace with your template engine (pug, handlebars, etc.) // Home route (works fine already) app.get('/', (req, res) => { res.render('index'); // Matches views/index.ejs }); // Add this route for /event app.get('/event', (req, res) => { res.render('event'); // Matches views/event.ejs }); app.listen(8080, () => { console.log('Server running on port 8080'); });
If you skip adding this route, Express has no idea how to handle the /event request, so it returns a 404.
Automatically Serve All Views in the views Folder
If you don't want to manually write a route for every new view you add, you can use Node's fs module to scan the views directory and auto-generate routes. Here's how:
const fs = require('fs'); const path = require('path'); // Define the path to your views folder const viewsDirectory = path.join(__dirname, 'views'); // Scan all files in the views folder fs.readdirSync(viewsDirectory).forEach(file => { // Get the filename without its extension (e.g., "event" from "event.ejs") const viewName = path.parse(file).name; // Create a route that matches the view name app.get(`/${viewName}`, (req, res) => { res.render(viewName); }); });
This will automatically create routes like /event, /about, /contact for every view file you add to the views folder.
Tweak Your Nginx Configuration
Your current Nginx config has a couple of small issues to fix:
- The
proxy_passpoints tohttp://www.ap.local.in:8080— unless you've explicitly mappedwww.ap.local.into your local machine in yourhostsfile, this might not resolve correctly. Usehttp://localhost:8080instead. - The
root /www/htmlline is unnecessary here, since all requests are being forwarded to your Node server. - You mentioned setting up HTTPS, but your config only includes an HTTP (port 80) server block. Here's a corrected, HTTPS-ready config:
# Redirect all HTTP traffic to HTTPS server { listen 80; listen [::]:80; server_name ap.local.in; return 301 https://$host$request_uri; } # HTTPS server block server { listen 443 ssl; listen [::]:443 ssl; server_name ap.local.in; # Path to your SSL certificate and key (use self-signed certs for local testing) ssl_certificate /path/to/your/certificate.pem; ssl_certificate_key /path/to/your/private-key.pem; location / { proxy_pass http://localhost:8080; # Add these headers to let Express know about the original request details proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
Quick Checklist to Test
- Restart your Express server after adding the
/eventroute or auto-routing code. - Reload Nginx to apply the config changes (
sudo nginx -s reloadon Linux/macOS). - Make sure your
hostsfile mapsap.local.into127.0.0.1(for local testing).
内容的提问来源于stack exchange,提问作者suresh

