I have created a functioning web app, which runs great on local host. Now I want this app to be accessible through any computer in the local network. I have server.js serving my ReactJS app (this works fine, other computers are able to access this)
const express = require('express');
const path = require('path');
const app = express();
app.use(express.static(path.join(__dirname, 'build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.listen(9000);
This app makes an API call to service (index.js) which stores data to mongodb. This is index.js
const express = require('express')
const bodyParser = require('body-parser')
var cors = require('cors')
const rakeRouter = require('./router/rake_analysis_router')
const mongoose = require('mongoose')
const app = express()
const apiPort = 5000
app.use(bodyParser.urlencoded({ extended: false }))
app.use(cors())
app.use(bodyParser.json())
mongoose.connect('mongodb://127.0.0.1:27017/rake_analysis_db', { useNewUrlParser: true })
const db = mongoose.connection
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('connected!');
});
app.use('/api', rakeRouter)
app.listen(apiPort, () => console.log(`Server running on port ${apiPort}`))
This is the part where it fails. I don't know what is going wrong here. Is it because the mongoDB server is hosted on localhost, or is this web service running only on localhost..? It would be great if someone could give an explanation of the problem and its possible solution.
I'd be happy to add more information if it is required.
/rake, payload).then(res => { window.alert(Rake inserted successfully); window.location.reload(false); }).catch(err => { window.alert(Could not save data. Please try again later.); })