-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
64 lines (49 loc) · 1.48 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
const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const path = require('path')
const bodyParser = require('body-parser')
require('dotenv').config()
const { spawn } = require('child_process')
// user routes
const mentees = require('./routes/api/mentees')
const mentors = require('./routes/api/mentors')
// server
const app = express()
// connect to database
const url = process.env.URL
mongoose
.connect(url)
.then(() => console.log('MongoDB connected...'))
.catch((err) => console.log(err))
// Bodyparse middleware
app.use(cors())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
// Routes
app.use('/api/mentees', mentees);
app.use('api/mentors', mentors);
app.get('/', (req, res) => {
res.send('Server is running!');
});
// middleware function to run python script
const runModel = (req, res, next) => {
const child = spawn('python', ['./python/process.py', JSON.stringify(obj)])
child.stdout.on('data', (data) => {
const str = data.toString().replace(/'/g, `"`)
const obj = JSON.parse(str)
// update label(s) for user
})
child.stderr.on('data', (data) => {
console.log(`stderr: ${data}`)
})
child.on('close', (code) => {
if (code !== 0) console.log('An error has occurred.')
console.log(`child process exited with code ${code}`)
})
next()
}
app.use(runModel);
// Launch server
const port = process.env.PORT || 5000
app.listen(port, () => console.log(`Listening on ${port}`))