-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (38 loc) · 1.46 KB
/
app.js
File metadata and controls
50 lines (38 loc) · 1.46 KB
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
44
45
46
47
48
49
50
const express = require('express');
const path = require('path');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const app = express();
// Importing dotenv to use environment variables
require('dotenv').config();
// Importing routes
const homeRoute = require('./api/routes/HomeRoute');
const demoRoute = require('./api/routes/DemoRoute');
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Set up the 'views' directory for template rendering using EJS
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// Enable request logging in the development environment
app.use(morgan('dev'));
// Parse incoming request bodies as JSON
app.use(bodyParser.json({ type: 'application/json' }));
// Parse incoming request bodies as TEXT
app.use(bodyParser.text({ type: 'text/*' }));
// Parse incoming request bodies as RAW
app.use(bodyParser.raw({ type: 'application/*' }));
// Define routes for the home and demo endpoints
app.use('/', homeRoute);
app.use('/demo', demoRoute);
// Middleware to handle 404 errors (route not found)
app.use((req, res, next) => {
const error = new Error('Not Found');
error.status = 404;
next(error);
});
// Middleware to handle other errors (e.g., internal server errors)
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({ error: error.message });
});
module.exports = app; // Export the Express application