1 | // Setup Express
|
---|
2 | const path = require('path');
|
---|
3 | const { spawn } = require('child_process');
|
---|
4 |
|
---|
5 | const express = require("express");
|
---|
6 | const cors = require("cors");
|
---|
7 |
|
---|
8 |
|
---|
9 | const app = express();
|
---|
10 |
|
---|
11 | const port = process.env.PORT || 3000;
|
---|
12 | const public_dir = path.join(__dirname, 'public')
|
---|
13 |
|
---|
14 |
|
---|
15 | app.use(cors());
|
---|
16 | app.use('/', express.static(public_dir))
|
---|
17 |
|
---|
18 | //// To help with POST and PUT requests to the server
|
---|
19 | //app.use(express.json());
|
---|
20 | //app.use(express.urlencoded({ extended: true }));
|
---|
21 |
|
---|
22 |
|
---|
23 | app.get("/", (req, res) => {
|
---|
24 | res.redirect("/index.html");
|
---|
25 | });
|
---|
26 |
|
---|
27 |
|
---|
28 | app.get("/api/get-doi", (req, res) => {
|
---|
29 | var doi = req.query.doi;
|
---|
30 |
|
---|
31 | var dataToSend;
|
---|
32 |
|
---|
33 | // Spawn new child process to call the python script
|
---|
34 | const python = spawn('python3', ['doi-stinky/oacore-get-doi.py', doi, "oacore-output.json"]);
|
---|
35 |
|
---|
36 | // Collect data from script
|
---|
37 | python.stdout.on('data', function (data) {
|
---|
38 | console.log('Pipe data from python script ...');
|
---|
39 | dataToSend = data.toString();
|
---|
40 | });
|
---|
41 |
|
---|
42 | // In close event we are sure that stream from child process is closed
|
---|
43 | python.on('close', (code) => {
|
---|
44 | console.log(`child process close all stdio with code ${code}`);
|
---|
45 |
|
---|
46 | //res.send(dataToSend)
|
---|
47 | res.sendFile("oacore-output.json", { root: __dirname });
|
---|
48 | });
|
---|
49 | });
|
---|
50 |
|
---|
51 |
|
---|
52 | app.listen(port, () => {
|
---|
53 | console.log(`Started CORS-enabled Express web server running on port ${port}.`);
|
---|
54 | });
|
---|