-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.ts
More file actions
48 lines (46 loc) · 1.61 KB
/
app.ts
File metadata and controls
48 lines (46 loc) · 1.61 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
import express from 'express';
const app = express();
import bodyParser from 'body-parser';
const port = 6600;
import dotenv from 'dotenv';
import { stablishConnection } from './db/connection';
import { exec } from 'child_process';
import schedule from 'node-schedule';
import { createServer } from 'node:http'; // createServer is from node:http
const server = createServer(app); // app is a callback function used to make servers {ie: http or https}
import { Server } from 'socket.io';
import Socket from './socket';
const io = new Server(server, {
cors: {
origin: ['http://localhost:5173','http://localhost:5174','https://pencil-room-client.vercel.app'],
methods: ['GET','POST']
}
})
Socket(io);
// cors
import cors from 'cors'
app.use(cors({
origin: ['http://localhost:5173','http://localhost:5174','https://pencil-room-client.vercel.app'],
methods: ['GET','POST']
}));
// dotenv
dotenv.config();
// stablish connection
stablishConnection();
// warming the server
app.get("/warm",(req,res)=>{
res.send("I'm here to keep the server warm");
})
const job = schedule.scheduleJob('* */14 * * * *', function(){ // sending request in every 14 min
// Send a request
if(process.env.NODE_ENV == 'production'){
exec('./scripts/warm.sh',(err,stdout,stderr)=>{})
}
});
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
server.listen(port,() => { // server provides the reusablity of the http servet to utilize the socket.io
process.stdout.write(`Server is up and running on port ${port}\n`);
})