forked from jtrudat/to-do-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
36 lines (27 loc) · 984 Bytes
/
server.js
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
33
34
35
36
//Dependencies
const express = require('express');
const mongoose = require('mongoose')
const cors = require("cors")
//Configuration
require('dotenv').config()
const app = express();
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true },
() => { console.log('connected to mongo: ', process.env.MONGO_URI) }
)
//Initialize middleware
app.use(express.json({ extended: false }));
app.use(cors({ origin: true, credentials: true }));
//Routes
app.get('/', (req, res) => res.send("Hello from server"))
const router = require('./routes/item-route-controller')
app.use('/', router);
//Errors
app.use('/*', (req, res)=>{
res.send('<h1><b>sorry item not found</b></h1><a href="http://localhost:3000"><button>redirect to listings</button></a>')
console.log('not routed, no item')
} )
// setting up port
const PORT = process.env.PORT || 4001;
app.listen(PORT, () => {
console.log(`server is running on http://localhost:${PORT}`);
});