[Fixed] Why is my very basic node.js endpoint timing out?

Issue

I have a very simple node.js app that looks like this:

const express = require('express');
const cors = require('cors');
require('dotenv').config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors);
app.use(express.json());

app.get('/', function(req,res) {
    return res.json({response: "ok"});
})

app.listen(port, () => {
    console.log(`Server is running on ${port}`);
})

When I run nodemon server, I get this response from my terminal:

A screenshot of my terminal showing that I'm connected to port 3000

When I try to hit localhost:3000/, I get this:

Screenshot of it trying to load a document?

And in postman:

Postman

I feel like I must have set up something wrong, as I’ve stripped out all the code and I’m still getting this error. Can anyone please advise?

Solution

It should be:

app.use(cors())

You have to invoke the function.

Leave a Reply

(*) Required, Your email will not be published