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:
When I try to hit localhost:3000/, I get this:
And in 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.