-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
104 lines (85 loc) · 3.57 KB
/
app.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// ###############################################################################
// # #
// # #### Mohammad Abdul Rafay ##### #
// # #### 99marafay@gmail.com ##### #
// # #### Github: rafay99-epic ##### #
// # #### The App.js File ##### #
// # #
// ###############################################################################
//Express is used to start the node server
const express = require("express");
// Default come with node js no need to install path.
const path = require("path");
//connection with database and fetching the connection file
const con = require("./database/connection");
const session = require("./session/session");
//adding the express session to the application.js session
// const session = require("express-session");
//Staring of the server
var app = express();
app.use(express.static("image"));
// Route to display static src images
app.get("/static", (req, res) => {
res.render("static");
});
app.use(
session({
secret: "ABCDefg",
resave: false,
saveUninitialized: true,
})
);
//this take two parameters __dirname is a node js variable.
//THis will provide a path to current directory.
const publicDirectory = path.join(__dirname, "./public");
//To use all the public iibiaries we need to tell the server to use the files.
app.use(express.static(publicDirectory));
//passing the url from the website of any data type.
app.use(express.urlencoded({ extend: false }));
//this line of code will make sure that the data is comming in the form of json.
app.use(express.json());
//setting the template for the website using handle bars
app.set("view engine", "hbs");
//define route for the webpage
//All of the webpages will be called from the roter folder and the pages.js fill will run to call the pages.
app.use("/", require("./routes/pages"));
//fetching the aurth file
app.use("/auth", require("./routes/auth"));
//The is port number from which the server will run and website will operate.
const port_website= process.env.port || 5000;
app.listen(port_website, () => {
console.log("Node Server is running at port 8080");
});
// ##### This part of the server will run th hardware and will display data from hardware to the website. ####
//Below This Are Socket.io Communication Donot Change Anything Except port
var http = require("http");
var fs = require("fs");
var index = fs.readFileSync("./views/view-parking.hbs");
const { SerialPort } = require("serialport");
const { ReadlineParser } = require("@serialport/parser-readline");
var port = new SerialPort({
path: "COM4", //Change This Port Only
baudRate: 9600,
dataBits: 8,
parity: "none",
stopBits: 1,
});
const parser = port.pipe(new ReadlineParser({ delimiter: "\r\n" }));
port.pipe(parser);
parser.on("data", function (data) {
// console.log(data);
});
var app = http.createServer(function (req, res) {
res.writeHead(200, { "Content-Type": "text/html" });
res.end(index);
});
const io = require("socket.io")(app);
parser.on("data", function (data) {
// console.log("Received data from port: " + data);
io.emit("data", data);
});
// const port_hardware= process.env.port || 3000;
//THis will be port number through which the derever for hardware will be Listening.
app.listen(3000, () => {
console.log("The server for the hardware is working on port 3000");
});