简体   繁体   中英

Unable to view locally running node app on mobile device

Totally new to node/express/mongo apps. I've got a basic one build running locally at localhost:3000 . No issues running locally.

However, when I grab my macbook's IP (both Macbook and iPhone on the same wifi network) and attempt to navigate to my local port with it, nothing happens. It eventually times out.

http://my.ip.address:3000 simply won't work on the mobile device.

What am I missing here?

Here's my server.js file if that's helpful.

 const express = require('express') const app = express() const bodyParser = require('body-parser') const MongoClient = require('mongodb').MongoClient var db // MONGO MongoClient.connect('mongodb://admin:almighty@ds035348.mlab.com:35348/caseyappv1', (err, database) => { if (err) return console.log(err) db = database app.listen(process.env.PORT || 3000, () => { console.log('View the build at http://localhost:3000/') }) }) app.set('view engine', 'ejs') app.use(bodyParser.urlencoded({extended: true})) app.use(bodyParser.json()) app.use(express.static('public')) app.get('/', (req, res) => { res.render('pages/index.ejs') }) 

Express uses the https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback under the hood.

You'll need to change app.listen to

app.listen(process.env.PORT || 3000, '0.0.0.0', () => {
    console.log('View the build at http://localhost:3000/')
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM