-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
43 lines (36 loc) · 1.52 KB
/
server.js
File metadata and controls
43 lines (36 loc) · 1.52 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
const express = require("express");
const path = require("path");
const sqlite3 = require("sqlite3").verbose();
const fs = require("fs");
const { router } = require(path.join(__dirname, "routes", "routes.js")); // ✅ Correctly importing only the router
const app = express();
const bodyParser = require('body-parser');
// Set the views directory (if not using default 'views' directory)
app.set('views', __dirname + '/views');
// Set the view engine (e.g., ejs)
app.set('view engine', 'ejs');
// Database setup with error handling
const dbPath = path.join(__dirname, ".database", "database.db");
console.log("Database path:", dbPath);
if (!fs.existsSync(dbPath)) {
console.error("Error: Database file not found at", dbPath);
process.exit(1);
}
// Middleware
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(bodyParser.urlencoded({ extended: true })); // To parse form submissions
app.use(bodyParser.json()); // To parse JSON bodies if needed
app.use(express.json());
app.use(express.static(path.join(__dirname, "public")));
app.use(express.static('public')); // ✅ Correctly serving static files
app.use('/', router); // mounted the router(routes.js - urls) under /
app.use(express.urlencoded({ extended: true })); // Add this line
// Start server (ONLY ONE app.listen)
const PORT = 8000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}/home`);
});