-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
48 lines (39 loc) · 1.62 KB
/
app.js
File metadata and controls
48 lines (39 loc) · 1.62 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
const express = require('express')
const path = require('path')
const app = express()
// Config file
const dotenv = require('dotenv')
dotenv.config()
// Set template engine
app.set('views', path.join(__dirname, 'public'))
app.set('view engine', 'ejs')
// Serve assets
app.use('/jquery', express.static(path.join(__dirname, 'node_modules/jquery/dist')))
app.use('/bootstrap', express.static(path.join(__dirname, 'node_modules/bootstrap/dist')))
app.use('/bootstrap-icons', express.static(path.join(__dirname, 'node_modules/bootstrap-icons/font')))
app.use('/sweetalert2', express.static(path.join(__dirname, 'node_modules/sweetalert2/dist')))
app.use('/socket-io', express.static(path.join(__dirname, 'node_modules/socket.io/client-dist')))
// Configure express
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname, 'public')))
// Handle errors
app.use((error, req, res, next) => res.status(error.status || 500).json({ error: error }))
// Init webcam
const livestream = require('rpi_camera_livestream')
livestream.register(app, process.env.PORT)
livestream.setPathname('/webcam')
livestream.setQuality(7)
livestream.start()
// Routes for requests
const router = require('./router')(livestream)
app.use('/', router)
// Enable web socket
const http = require('http').Server(app)
const io = require('socket.io')(http)
io.on('connection', socket => {
console.log('User connected to socket')
livestream.camera.on('frame', data => socket.emit('stream', data))
})
// Start HTTP server
http.listen(process.env.PORT, () => console.log(`Server is up and listen on port ${process.env.PORT}`))