You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 /event that renders the corresponding view file. For example, if your view is views/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:

  1. The proxy_pass points to http://www.ap.local.in:8080 — unless you've explicitly mapped www.ap.local.in to your local machine in your hosts file, this might not resolve correctly. Use http://localhost:8080 instead.
  2. The root /www/html line is unnecessary here, since all requests are being forwarded to your Node server.
  3. 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

  1. Restart your Express server after adding the /event route or auto-routing code.
  2. Reload Nginx to apply the config changes (sudo nginx -s reload on Linux/macOS).
  3. Make sure your hosts file maps ap.local.in to 127.0.0.1 (for local testing).

内容的提问来源于stack exchange,提问作者suresh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:51:31