-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
64 lines (51 loc) · 1.44 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
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const path = require("path");
const Sentiment = require("sentiment");
const config = require("./helper/config");
const formRoute = require("./routes/api/serve.form");
const app = express();
//adding socket.io
const server = require("http").Server(app);
const io = require("socket.io")(server);
const PORT = process.env.PORT || 8000;
//connecting mongodb
mongoose
.connect(config.mongoURL)
.then(() => {
console.log(`connected.....`);
})
.catch(err => {
console.error(err);
});
app.set("view engine", "ejs");
//serving static files
app.use(express.static(path.join(__dirname, "/public")));
//using bodyparser
app.use(
bodyParser.urlencoded({
extended: false
})
);
//Route in api/serve.form.js
app.use(formRoute);
//establishing connection for socket.io
io.on("connection", socket => {
console.log(`connection established....`);
socket.on("runanaysis", text => {
const sentiment = new Sentiment();
const result = sentiment.analyze(text);
console.log(result);
const { score, comparative, tokens, words, positive, negative } = result;
console.log("socket", result);
socket.emit("result", result);
});
socket.on("disconnection", () => {
console.log("disconnected....io...");
});
});
server.listen(PORT, err => {
if (err) throw err;
console.log(`server is running at ${PORT}`);
});