source: other-projects/bib-stinky/trunk/nodejs-server/express-web-server.js@ 36333

Last change on this file since 36333 was 36333, checked in by davidb, 21 months ago

Sending back the JSON from a get-doi python script

File size: 1.3 KB
Line 
1// Setup Express
2const path = require('path');
3const { spawn } = require('child_process');
4
5const express = require("express");
6const cors = require("cors");
7
8
9const app = express();
10
11const port = process.env.PORT || 3000;
12const public_dir = path.join(__dirname, 'public')
13
14
15app.use(cors());
16app.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
23app.get("/", (req, res) => {
24 res.redirect("/index.html");
25});
26
27
28app.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
52app.listen(port, () => {
53 console.log(`Started CORS-enabled Express web server running on port ${port}.`);
54});
Note: See TracBrowser for help on using the repository browser.