问题
I'm using loopback.io which base on expressjs and I tried to add port as the first parameter of app.listen
like this:
// server.js
app.start = function() {
// start the web server
return app.listen(80, function() {
app.emit('started');
console.log('Web server listening at: %s', app.get('url'));
});
};
But it doesn't work.
I have been searching for this for awhile but I haven't found the solution yet.
回答1:
Change the port
property in server/config.json
. See https://github.com/strongloop/loopback-sandbox/blob/master/server/config.json#L4
回答2:
This is similar to problems seen in Express; one such answer is here Node.js + Express: app won't start listening on port 80
I don't see it explicitly noted in the documentation, but have also had the issue where listening on port 80 required sudo (root). The loopback.js documentation https://apidocs.strongloop.com/loopback/#app-listen states that your call is just passed to the HTTP Server class https://nodejs.org/api/http.html#http_server_listen_port_hostname_backlog_callback but your format also matches that of Express (hence the first link).
Your error message would be helpful in knowing if root access is the problem or if it's something else.
回答3:
I needed to set the port dynamically so that it would be 3001 in development and 80 in production. This is what worked for me:
app.start = function() {
//change the port if needed for production
if (process.env.PORT) {
app.set('port', process.env.PORT);
}
// start the web server
return app.listen(function() {
...
回答4:
{
"restApiRoot": "/api",
"host": "0.0.0.0",
"port": 80,
"remoting": {
"context": false,
"rest": {
"normalizeHttpPath": false,
"xml": false
},
"json": {
"limit": "50mb"
},
"urlencoded": {
"limit": "50mb",
"extended": true
},
"handleErrors": false
},
"legacyExplorer": false,
"logoutSessionsOnSensitiveChanges": true
}
Please refer to this server/config.json
sample.
回答5:
If you are using loopback x3 you need to create a file called server/config.local.json
. Then copy all files in server/config.json
into it. Now, add "your port number" you need to the "port property" in server/config.local.json
and it will work.
For detail, see the original source: https://loopback.io/doc/en/lb3/Environment-specific-configuration.html#application-wide-configuration
来源:https://stackoverflow.com/questions/32430773/how-can-i-run-loopback-application-on-port-80