-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.07 KB
/
index.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
42
43
import express from 'express';
import path from 'path';
import { fileURLToPath } from 'url';
/*
Get environment variables.
*/
const PORT = process.env.PORT || 3000;
const NODE_ENV = process.env.NODE_ENV || "development";
/*
Setup application.
*/
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// setup routes
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
app.use("/", express.static(path.join(__dirname, 'public')));
// catch any undefined routes
app.all('*', (request, response) => {
console.log('Returning a 404 from the catch-all route');
return response.sendStatus(404);
});
// start application
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use((req, res) => {
res.send('Welcome to Express');
});
app.listen(PORT, () => {
console.log(`Server running on port ${ PORT }`)
});