0

I´m having a problem with vercel. I'm done trying everything.

I tried to deploy, multiple times, my node app with express and nothing works, I always get:

" 404: NOT_FOUND Code: NOT_FOUND "

Here's my index.js:

const exp = require('constants');
const express = require('express');
const res = require('express/lib/response');
const app = express();
const path = require('path');

//express server port

app.listen(process.env.PORT || 3000, () =>{
 console.log("Server status: Online.")
});

app.get("/", (req, res) => {
    res.sendFile(path.join(__dirname, "/views/index.html"));
});

app.get("/register", (req, res) => {
    res.sendFile(path.join(__dirname, "/views/register.html"));
});

app.get("/login", (req, res) => {
    res.sendFile(path.join(__dirname, "/views/login.html"));
});

app.use(express.static("public"));

and my vercel.json:

{
    "version": 2,
    "builds": [
        {
            "src": "./app.js",
            "use": "@vercel/node"
        }
    ],

    "routes": [
        {
            "src": "/(.*)",
            "dest": "/"
        }
    ]
}

I tried deleting node_modules, changing "app.js" to "index.js", almost everything, but nothing worked.

2 Answers 2

1

Issue

I have some recommendations for anyone that is trying to serve basic html page files with javascript and is facing this 404 issue only in production and not locally. A good point to begin debugging is to take a look at what Vercel is receiving and exposing to your public domain.

Configuration

In the Vercel dashboard: visit your project at projectID -> Source tab. From here, take a look at the tabs present on the left side titled Source & Output.

The files present in Output will be available publicly, you may see index & middleware already in there. Vercel will usually be able to locate your index.html from the .root and will automatically generate the middleware if one isn't present.

The Source tab is specifying the files that will be publicly available when accessing your domain. If the .html page you're trying to locate isn't in this Ouput tab that is probably the source of your problems. We'll need to ensure the .html page you're trying to contact exists in this Output directory.

Solution

How can we do this?

Ensure you have a /public folder in your project. Your index.html should be in the .root directory, along with dependency managers and lock files. By moving files over we're telling Vercel to make these available publicly, aka in the Output directory.

Ensure your directory assembles something like the following:

Directory structure

.root/
│
├── package.json
├── package-lock.json
├── index.html
├── vercel.json
├── public/
│   ├── styles.css
│   ├── images/
│   └── page1.html
  

Additional recommendations (cleaning the url):

vercel.json

{
  "cleanUrls": true
}

Utilize a cleanUrls object in your vercel.json to clean the URL:

eg. https://your-website.com/about.html -> https://your-website.com/about

Sign up to request clarification or add additional context in comments.

Comments

0

you can change index.js to app.js

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.