-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
44 lines (37 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
44
const express = require('express');
const PORT = 4000;
const productRouter = require('./routes/productRoutes');
const hbs = require('hbs');
const productData = require('./data/products.json')
const { logData } = require('./middlewares/logger')
require('dotenv').config();
const connectDatabase = require('./database/connection')
//Creating a server
const app = express();
//Connect Database
connectDatabase();
//Setting view engine
app.set('view engine', 'hbs');
hbs.registerPartials('./views/partials')
//Serving static file
app.use(express.static('./public'))
app.use(express.json())
//Custom middleware
// app.use(logData);
// app.use('/api/products', logData);
//Routes
app.get('/', (req, res) => {
res.send("Welcome to Ecommerce API")
})
//SSR Routes
app.get('/products', (req, res) => {
res.render('index', { title : "Online Store", productData});
})
app.use('/api/products', productRouter);
//Error route handling
app.get('*', (req, res) => {
res.status(404).send("Sorry, Page Not Found!")
})
app.listen(PORT, () => {
console.log(`Server started at port ${PORT}`)
})