-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.ts
35 lines (26 loc) · 890 Bytes
/
server.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import express, { Express, Request, Response } from "express";
import dotenv from "dotenv"
import mongoose, { ConnectOptions } from "mongoose";
import itemsRouter from './routes/items.js';
import tagsRouter from './routes/tags.js';
import cors from 'cors';
// configure cors to allow any origin
const corsOptions = {
origin: '*',
optionsSuccessStatus: 200
}
dotenv.config();
const app: Express = express();
const host = process.env.HOST;
const port = process.env.PORT;
const databaseURL = process.env.DATABASE_URL ?? '';
mongoose.connect(databaseURL, {});
const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to Database'));
app.use(cors());
app.use('/items', itemsRouter);
app.use('/tags', tagsRouter);
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});