Adding a cors package makes my express server not load

Issue

So I’ve set up a simple express.js app:

import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';



dotenv.config();

const PORT: string | number = process.env.PORT || 8000;
const app: Express = express();


app.use(express.json());
app.use(express.urlencoded({ extended: true }));


app.get('/', (req: Request, res: Response) => {
  res.send('Simple server');
});

app.listen(PORT, () => console.log(`Running on ${PORT} ⚡`));

which seems to be working as expected:enter image description here

But after I try to add a cors package (to manage cors in future):

import express, { Express, Request, Response } from 'express';
import dotenv from 'dotenv';
import cors from 'cors';


dotenv.config();

const PORT: string | number = process.env.PORT || 8000;
const app: Express = express();

app.use(cors);

app.use(express.json());
app.use(express.urlencoded({ extended: true }));


app.get('/', (req: Request, res: Response) => {
  res.send('Simple server');
});

app.listen(PORT, () => console.log(`Running on ${PORT} ⚡`));

my server seems to be somehow "stuck" and is not loading at all:
enter image description here

What’s up with that? How do I fix this? Thanks in advance!

Solution

You want

app.use(cors());

and you have

app.use(cors);   /* wrong */

Answered By – O. Jones

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published