Reputation: 2932
I'm trying to run the simplest of node.js apps on a Azure App Service (Linux) but I'm out of idéas on how to get this running. I've been trying different approaches and nothing seems to work.
I'm simply deploying the app through FTP and putting the files under "wwwroot" but when requesting the url it loads forever and then ends up with a service unavailable response.
When creating the app I choose Linux as platform, runtime node.js 8.11 and deploy through "code" and not docker.
In the logs I can see this:
docker run -d -p 46852:8080 --name [appname] -e WEBSITE_NODE_DEFAULT_VERSION=8.11 -e APPSETTING_WEBSITE_NODE_DEFAULT_VERSION=8.11 -e WEBSITE_SITE_NAME=[appname] -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_INSTANCE_ID=[id] -e HTTP_LOGGING_ENABLED=1 appsvc/node:8.11.2_1805212105
Container [appname] for site [appname] did not start within expected time limit. Elapsed time = 230.4882261 sec
What does this mean, is docker being used somehow even though I setup the app with deploy through "Code" and FTP.
package.json
{
"name": "foo",
"version": "1.0.0",
"description": "x",
"author": "x",
"private": true,
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.16.3"
}
}
server.js
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
const host = process.env.HOST || '127.0.0.1'
app.get('/', (req, res) => res.send('Hello!'))
app.listen(port, host)
I've also tried ssh into the app and running npm install
. I've tried running the app through ssh console node server.js
but ends up with the message "Port already in use". I've tried setting the port to 8080 in "application settings" in Azure Portal.
What am I missing here? How do I get the app running?
Upvotes: 3
Views: 10094
Reputation: 2178
For anyone struggling with this, I'd suggest reading through this article on Azure documentation.
The things that caught me were...
1.) Not setting the port correctly, make sure to set the port to the PORT ernvironment variable, or 8080, which seems to be the default for Azure Web Apps.
import express from 'express';
const app = express()
const port = process.env.PORT || 8080
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
2.) On dev/local environment, use tsc && node dist/app.js
to compile your TypeScript and run the output dist/app.js
.
THIS WORKS FOR DEV/LOCAL ONLY AND WILL NOT RUN ON AZURE.
3.) When deploying to Azure, you need to change it to run with PM2.
pm2 start dist/app.js --no-daemon
4.) Your scripts section in package.json should look something like this...
"scripts": {
"build": "tsc",
"dev": "tsc && node dist/app.js",
"start":"pm2 start dist/app.js --no-daemon"
}
If you're deploying with GitHub actions, then your .yaml file npm install, build and test section should look like this...
- name: npm install, build, and test
run: |
npm install
npm run build --if-present
#npm run test --if-present
Azure Web Apps will automatically run npm start
on deploy, so make sure your start script runs pm2 as I mentioned above.
Upvotes: 1
Reputation: 2099
In my site, I just did app.listen(process.env.port);
- without the host argument. And try doing npm start
.
Also check web.config
and iisnode.yml
which are generated automatically and are both necessary to run Node.
Upvotes: 3
Reputation: 1109
First off, it would be good to know how exactly are you creating your application (tools used, steps taken etc.). Overall, the main thing to understand - Azure App Service does not use Node.js by default, i.e. once you provisioned it. To specify that the host will be running Node app, you need to set 'WEBSITE_NODE_DEFAULT_VERSION' parameter (in Application Settings blade in Azure App Service). Once done, you can deploy with any available option (using publish functionality, local git, using Azure DevOps etc.). Here is a good topic to start with. Getting straight to the point:
Upvotes: 1