-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
69 lines (47 loc) · 2.04 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const express = require('express'); // getting express from our node_molues
const path = require('path');
const app = express(); // calling our express function here
const mysql = require('mysql'); // getting mysql from node_molues
const dotenv = require('dotenv'); // getting dotenv from node_modules
const cookieParser = require('cookie-parser');
dotenv.config({ path : './.env'}) // as we dont want to share our database user and pass so .env ( we can name it as any like xyz.env its just it should have extension of .env)
const PORT = process.env.PORT || 3002; // creating a port where our server should start
//creating connection with our databae
const db = mysql.createConnection({
host: process.env.DATABASE_HOST, // we are running our database on locally so local host , if it wasn't local host add the api here
user: process.env.DATABASE_USER, // user is root
password: process.env.DATABASE_PASSWORD, // passwd
database: process.env.DATABASE // add here database name
});
//connecting our databse and checking everything works fine
db.connect( (error)=>{
if(error){
console.log(error)
}
else{
console.log("MySQL database connected.....")
}
})
const publicDirectory = path.join(__dirname , './public');
app.use(express.static(publicDirectory));
// console.log(__dirname)
//parse URL-encoded bodies ( as sent by HTML forms )
app.use(express.urlencoded({ extended : false }));
//pasrse JSON bodies( as sent by API clients )
app.use(express.json());
app.use(cookieParser());
//setting our view engine , it will allows how we render our html
// app.set('view engine' , 'ejs');
// app.set('view engine', 'ejs');
app.set('view engine', 'hbs');
//define routes
app.use('/' , require('./routes/pages'));
// app.use('/register' , require('./routes/pages'));
app.use('/auth' , require('./routes/auth'))
//listening our express() here
app.listen(PORT , () => {
console.log(`Server started on Port : ${PORT}`)
});
// <!-- <% if (message) { %>
// <h4 class="alert alert-danger mt-4"><% message %></h4>
// <%}%> -->