-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
70 lines (56 loc) · 1.92 KB
/
app.js
File metadata and controls
70 lines (56 loc) · 1.92 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import dotenv from 'dotenv'
import express from 'express'
import cors from 'cors';
import connectDB from './config/connectdb.js'
import userRoutes from './routes/userRoutes.js'
import webRoutes from './routes/web.js'
import adminRoutes from './routes/adminRouter.js'
import path from 'path'
import ejs from 'ejs'
import WebSocket from 'ws'
import { NlpManager } from 'node-nlp'
import { WebSocketServer } from 'ws'
dotenv.config()
const app = express()
const port = process.env.PORT
const DATABASE_URL = process.env.DATABASE_URL
// CORS Policy
app.use(cors())
// Database Connection
connectDB(DATABASE_URL)
// JSON
app.use(express.json())
// view engine
app.set("view engine","ejs");
app.use(express.urlencoded({ extended: false }));
// Load Routes
app.use("/user", userRoutes);
app.use("/admin",adminRoutes);
app.use("/",webRoutes);
const staticPath = path.join(process.cwd(), 'static');
app.use('/static', express.static(staticPath));
// app.use('/static', express.static(path.join(__dirname, 'static')))
const manager = new NlpManager({ languages: ['en'] });
manager.load();
const server = new WebSocketServer({ port: 8080 });
server.on('connection', (socket) => {
console.log('Client connected');
socket.on('message', async (message) => {
console.log(`Received message: ${message}`);
// Use the NlpManager to process the message
console.log(`${message}`.length);
var msg = JSON.stringify(message);
const response = await manager.process("en",`${message}`);
if(response.answers.length<=0){
socket.send("Sorry! we are unable to process this request at current moment.Please contact at Policyfinder2@gmail.com");
}
else
socket.send(response.answer);
});
socket.on('close', () => {
console.log('Client disconnected');
});
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`)
})