-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
149 lines (116 loc) · 3.9 KB
/
server.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"use strict";
/*****************************
Dependencies loading
*****************************/
var express = require('express');
// This is for getting easily the input from web POST method forms
var bodyParser = require('body-parser');
var ia = require('./ia/me.js').getInstance();
// This is to sanitize the above input
var expressValidator = require('express-validator');
// This is to allow connexion to the server from mobile phone for example
var cors = require('cors');
var app = express();
/*****************************
Server parameters definition
******************************/
// the ./public directory will be parsed when js script or images are loaded through html pages
app.use('/img', express.static( __dirname+'/public/img' ) );
app.use('/js', express.static( __dirname+'/public/js' ) );
app.use('/css', express.static( __dirname+'/public/css' ) );
// bodyparser is used to get "POST" parameters from a form
// Validator is a sanitizer for input fields
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(expressValidator());
app.use(cors());
// ejs is used to render html pages !
app.engine('ejs', require('ejs').renderFile);
app.set('views', __dirname+'/views');
app.set('view engine', 'ejs');
/*****************************
Custom variables definition
******************************/
var port = process.env.PORT || 8081;
console.log('will listen on port '+port);
/*****************************
URL param
******************************
app.param('voc_wordToFind', function (request, response, next, userto) {
//look for the word in the available voc
console.log('mot à chercherrécupéré !');
request.wordToFind = 'test';
return next();
});
*/
/*****************************
Router
*****************************/
// display the form to search all information about a particular word
app.get('/', function(req, res) {
res.render('page_chat', {"status":"init", "info":[]});
});
// get a word trhough POST method and disp information about this word
app.post('/', function(req, res) {
var s = req.body.sentence;
if(s){
ia.analyze(s).then(
function(analyzedSentences){
var nbSentences = analyzedSentences.length;
var infoS =[];
for(var indexS=0; indexS<nbSentences; indexS++){
var nbWords = analyzedSentences[indexS].words.length;
var infoW = [];
for(var indexW=0; indexW<nbWords; indexW++){
infoW.push(analyzedSentences[indexS].words[indexW].formatted());
}
infoS.push(infoW);
}
res.render('page_chat', {"status":"ok", "info":infoS});
return true;
},
function(err){
res.render('page_chat', {"status":"error", "info":err});
return true;
}
).done();
}
else{
res.render('page_chat', {"status":"noInput", "info":""});
}
});
// index page, the frameslist.json file is read and frames are displayed on the page
app.get('/about', function(req, res) {
res.render('page_about');
});
// 404 handling
app.use(function(req, res, next){
res.status(404).render('page_error_not_found');
});
// When the server must be shut down, some operations can be executed
var gracefulShutdown = function(){
console.log('Server [off]');
process.exit();
};
// listen for TERM signal .e.g. kill
process.on ('SIGTERM', gracefulShutdown);
// listen for INT signal e.g. Ctrl-C
process.on ('SIGINT', gracefulShutdown);
/*****************************
Launching
*****************************/
// All its needs are prepared
ia.prepare().then(
function(value){
if(value === 'ready'){
app.listen(port);
console.log('Server [on]');
}
else{
console.log('Server [off]');
}
},
function(err){
console.log(err+'\nServer [off]');
}
).done();