0

Here is my server.js

const port = process.env.PORT || 5000; const express = require('express'); const bodyParser = require('body-parser'); const multer = require('multer'); const uuidv4 = require('uuid/v4'); const path = require('path'); const htmlparser = require("htmlparser2"); const fs = require("fs"); let filename = ''; // configure storage const storage = multer.diskStorage({ destination: (req, file, cb) => { /* Files will be saved in the 'uploads' directory. Make sure this directory already exists! */ cb(null, './uploads'); }, filename: (req, file, cb) => { /* uuidv4() will generate a random ID that we'll use for the new filename. We use path.extname() to get the extension from the original file name and add that to the new generated ID. These combined will create the file name used to save the file on the server and will be available as req.file.pathname in the router handler. */ const newFilename = `${uuidv4()}${path.extname(file.originalname)}`; cb(null, newFilename); }, }); // create the multer instance that will be used to upload/save the file const upload = multer({ storage }); const app = express(); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.get("/upload", (req, res) => { res.sendFile(__dirname + "/index.html"); }); app.post('/upload', upload.single('selectedFile'), (req, res) => { let summary =''; let counter =0; let times = []; let days = []; let building = []; let date = []; let isGood = false; filename = req.file.filename; fs.readFile('./uploads/'+filename, 'utf8', function(err, data) { if (err) throw err; //console.log(data); const parser = new htmlparser.Parser({ onopentag: function(name, attribs){ // summary="This table lists the scheduled meeting times and assigned instructors for this class.." if(name === "tr" && attribs === 'summary'){ //console.log(attribs); } }, ontext: function(text){ if(text==='Class'){ isGood=true; counter++; } if(text!="Lecture"&&text!="Class"&&isGood){ if(counter===2){ times.push(text); } if(counter===4){ days.push(text); } if(counter===6){ building.push(text); } if(counter===8){ date.push(text); } counter++; console.log(text); console.log(counter); summary = summary+text; } if(text==="Lecture"){ isGood=false; counter=0; } }, }, {decodeEntities: true}); parser.write(data); parser.end(); console.log("STTTTTTTTTTTTTTTTTTTTTTTAAAAAAAAAAAAAAAAAAAAAAAAAARRRRRRRRRRRRRRRRRRRRRTTTTTTTTTTTTTTT"); console.log(building); console.log(times); console.log(date); console.log(days); fs.unlink('./uploads/'+filename, function(error) { if (error) { throw error; } console.log('Deleted filename', filename); }) }); /* We now have a new req.file object here. At this point the file has been saved and the req.file.filename value will be the name returned by the filename() function defined in the diskStorage configuration. Other form fields are available here in req.body. */ res.send(); }); app.listen(port, () => console.log(`Server listening on port ${port}`)); 

I can get this to run while running "npm start" first then this. This doesn't check if anything else is running on port 3000 so they both are able to run on port 3000. I don't like this and I don't know if this will work once we build our React App and begin hosting it. How can I combine this code with react app to get it to run on a single server instead of two? I've tried literally combining start.js and this code, however start.js is obviously just for development purposes and not the final product. How can I serve up my html to the user and still have the front end sending to the back end.

3
  • The question is way too vague for stack overflow... Commented Nov 13, 2018 at 0:12
  • What details would you have liked. Commented Nov 13, 2018 at 0:23
  • 1
    stackoverflow.com/help/mcve Commented Nov 13, 2018 at 0:27

1 Answer 1

1

Suprisingly enough I had come here after researching this for hours and immediately after posting the question if found the answer. The simple answer is: add "proxy": "http://localhost:PORT"

where PORT = express server port it is running on I still however don't know if this works with the build product however it works with the quick start server.

All credit goes to this lovely website https://dev.to/loujaybee/using-create-react-app-with-express

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.