-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (31 loc) · 918 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
37
38
39
40
41
// Imports
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const router = require("./routes/index");
const db = require("./database/database");
const dotenv = require("dotenv");
// Basic Configuration, Envirnment variables and db connection
dotenv.config();
db.connect();
const port = process.env.PORT || 3000;
const app = express();
app.use(cors({optionsSuccessStatus: 200}));
app.use(bodyParser.urlencoded({
extended: false
}));
// Static files
app.use(express.static(`${__dirname}/public`));
// Routing
app.get('/', function(req, res) {
res.sendFile(`${__dirname}/views/index.html`);
});
app.use('/api/shorturl', router);
// No matching route
app.use(function(req, res) {
res.status(404).sendFile(`${__dirname}/views/404.html`);
});
// Server listening
app.listen(port, function() {
console.log(`Server listening on port ${port}`);
});