skip to Main Content

I have my Node application deployed on Vercel: node-demo-ashen.vercel.app. The problem I am encountering is that on localhost:3000 the images are loading fine, but my images are not loading on Vercel. I have also configured my vercel.json file several times:
{
"version": 2,
"builds": [
{
"src": "server.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/images/(.)",
"dest": "/public/images/$1"
},
{
"src": "/(.
)",
"dest": "/server.js"
}
] }
Help me fix this issue.

I have tried configuring the vercel.json file and also tried logging out and logging in again to resolve the issue, but it seems that it is not working. The images are loading fine on localhost, but on Vercel, images are not loading at all.

2

Answers


  1. Chosen as BEST ANSWER
    const express = require('express');
    const path = require('path');
    
    const app = express();
    
    // Set the view engine
    app.set('view engine', 'ejs'); // Change 'ejs' to your view engine if different
    
    // Set the views directory
    app.set('views', path.join(__dirname, 'views'));
    
    app.get('/', (req, res) => {
        res.render('index'); // Ensure 'index' matches your view file name
    });
    
    // Serve static files from the 'public' directory
    app.use(express.static(path.join(__dirname, 'public')));
    
    // Import routes from routes.js
    const routes = require('./routes');
    app.use('/', routes);
    
    const PORT = process.env.PORT || 3000;
    app.listen(PORT, () => {
        console.log(`Server running on port ${PORT}`);
    });
    

  2. I think you might be missing

    app.use(express.static(path.join(__dirname, 'public')));
    

    in your nodejs file. If not do share your nodejs file so that I can help

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search