Cannot set an IP address on Express JS app

Multi tool use


Cannot set an IP address on Express JS app
Trying to test an endpoint in express But keep getting 404 error.
var express = require("express")
var app = express()
//var http = require('http').Server(app)
app.get('/', function(req,res){
res.send('ok from end point')
})
var port = process.env.PORT|| 8080
var localhost = 'someLocalHost.med.gov'
console.log({'localhost':localhost,
'post':port})
//
app.listen(port,localhost,function(err){
if (err){
console.log('err')
}
else {
console.log('Listening')
}
})
||
process.env.PORT
8080
process.env.PORT
8080
Perhaps you need to read about how DNS works. Your server runs at an IP address and a client connects to an IP address. It is DNS that allows a client to lookup
somehost.med.gov
and find out the IP address that it should use to connect with.– jfriend00
yesterday
somehost.med.gov
2 Answers
2
Localhost refers to 127.0.0.1
. You can't just launch a server on any address that you want. If you're wanting to override localhost
you can look into modifying your HOSTS
file locally to setup an alias for localhost
.
127.0.0.1
localhost
HOSTS
localhost
So I ended up http:// IP_ADDRESS:8080 and that took care of it.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Look up the
||
operator. You are doingprocess.env.PORT
OR8080
. In other words, ifprocess.env.PORT
is set, that's the value that will be used, and8080
will only be used if the former is not set.– Claies
yesterday