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:
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:
What's up with that? How do I fix this? Thanks in advance!
CodePudding user response:
You want
app.use(cors());
and you have
app.use(cors); /* wrong */