nestjs to azure windows app service - web.config settings - node.js

I've been trying to deploy my nestjs api to azure since yesterday and have not been successful. At first I was trying to push it to a linux cloud service plan and was able to deploy it from vs code, see in the azure logs that the app was successfully started, but it would not return any data from my endpoints and after some digging, I kept seeing some container failures. I tried troubleshooting that for a while then decided I would try setting it up on a windows app service instead.
Following this guide: https://learn.microsoft.com/en-us/azure/app-service/app-service-web-get-started-nodejs, I pushed my nestjs app up to an azure app service on windows and again, it did not work. I pulled down the repo in that article to compare it to mine and they have the entry point in the web.config for the nodejs app pointing to index.js. That index.js doesn't exist in my nestjs app and I did some research and found out that the entry point for a nestjs app after everything is compiled is main.js. There is another difference in that sample project and my nestjs app in that the nestjs app's main.js file appears to be in a dist folder, rather than at the root of the wwwroot path.
My nestjs app obviously didn't contain a web.config out of the box so I created one using the hello world repo's as an example. I updated the iisnode path to dist/main.js and the rewrite path as well to the same path/file since that's the only thing I can think to do.
The only other wildcard is the node version number that I'm changing in azure for the WEBSITE_NODE_DEFAULT_VERSION application setting...I'm not sure what that should be for my nestjs app. My current local version is 12.16.2 but when I change the version to that on azure, it doesn't work.
I'm not sure what else to try here - I'd be fine trying to go back to linux if I could sort out the docker container issue but that seemed like a dead end. I think I'll likely have more luck continuing down the windows route if I can just get this web.config / version thing to work.
Is main.js in the dist folder where I should be pointing my web.config? Is there a max version limit on azure? Is there anything else I might be missing?
EDIT: After turning on streaming logs - I'm now seeing this exception
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object. (D:\Program Files (x86)\iisnode\interceptor.js:459:1)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
after some googling, I've found that exception was common with older versions of node (< 4). I'm well beyond that version and have azure set to a version well beyond that as well.

I got it sorted out with the help of a friend. Running this in the azure cli got things sorted out using the linux setup.
az webapp config set --resource-group myResourceGroup --name myAppServiceName --startup-file "npm run start:prod"

Related

Can't run script of MongoDB Atlas URL using node to database

I'm currently following along with a MDN tutorial Express Local Library Part 3. I've done everything correctly, that I know of, so far. I made a database then a cluster with MongoDB Atlas and am now trying to upload my data from a file populatedb.js by command node populatedb <my MongoDB Atlas URL here> but it doesn't work at all. I've linked it to the apps.js file with the correct URL (or so I think).
I'm getting this returned in the console:
___________-MacBook-Air express-locallibrary-tutorial % node populatedb 'mongodb+srv://_________:_______________#cluster0.mhzo6.mongodb.net/local_library?retryWrites=true&w=majority'
internal/modules/cjs/loader.js:905
throw err;
^
Error: Cannot find module '/Users/________/Desktop/express-locallibrary-tutorial/populatedb'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:902:15)
at Function.Module._load (internal/modules/cjs/loader.js:746:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at internal/main/run_main_module.js:17:47 {
code: 'MODULE_NOT_FOUND',
requireStack: []
}
I've even deleted my database, cluster, organization and everything and remade them exactly the same but still to no avail.
Here is the answer:
[for those of you stumped on this tutorial this will most likely be the cause!]
The tutorial made it very easy to misunderstand and create a secondary folder of the same name as the root in the root...[or perhaps and more likely its that we are still beginners and its very easy to misunderstand and make this simple mistake.] In creating the initial express project https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website at tutorial 2 "creating the project" I had accidentally made the simple mistake of creating my own express-locallibrary-tutorial folder/project in VS code then ran all of the cmds from the integrated terminal instead of the external terminal. This is what caused the double folder.
I was easily able to work around this just by making sure I was in the correct folder when running node/nodemon and made sure to path correctly for all routes just to "cover up" the mistake which I believed was no big deal... This caused me to have to trash my entire project. I had actually finished this tutorial but once it came to launching it to Heroku I quickly realized that it wasn't worth it to attempt to fix everything and best to restart it from the beginning because Heroku can only run scripts from the /root/ as it should [at least so far since this beginner/noob has been troubleshooting that is!]
So watch out for that simple mistake all! If you did it too and caught it early it is definitely worth the headache of restarting because it will help you in the long run and is a good lesson of a simple mistake, or it is for me!!

Use of const in strict mode : Azure Web App

Application has thrown an uncaught exception and is terminated: SyntaxError: Use of const in strict mode.
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\mongoose\index.js:7:18)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
Error is thrown everytime Azure Web App project is made and an Express App is published. I tried with Azure Express as well as Blank nodeJs App to do it
Looks like it's caused by an invalid node version on Azure. Go to Azure portal, your Web App - Application settings, check WEBSITE_NODE_DEFAULT_VERSION.
Once we specify a version not available on Azure, a quite old version 0.10.40 is in use, where const is not enabled by default so that we met SyntaxError: Use of const in strict mode. See related thread for more details.
We can use 10.6.0, 8.11.1, etc. Go to https://<yourwebappname>.scm.azurewebsites.net/api/diagnostics/runtime to see all versions available.
Caveat by Clinkz
In some cases, the above solution may not work. This may be because your project includes the file iisnode.yml. If this file exists, it overrides application settings environment variable. The content of this file should be as follows :
nodeProcessCommandLine: "%SystemDrive%\Program Files (x86)\nodejs\0.10.4\node.exe"
The node version specified here takes precendence. To fix it, simply update the version, like so and deploy:
nodeProcessCommandLine: "%SystemDrive%\Program Files (x86)\nodejs\8.9.4\node.exe"
Refer to this.
To conclude, the priority: iisnode.yml > package.json(engine)> Application settings. Application setting is recommended as it's easy to check and modify on portal.

Deploying Loopback Node.js server using VSTS and Azure deployment

We're a small team of developers and we want to develop a RESTful API service for our project. We would like to use Visual Studio for our Node.js Loopback framework development and we want to use git on VSTS for our version control. We found out that there are ways to automate build and deployment on VSTS to Azure Web app, but we're having problems creating a loopback project, creating a Visual Studio solution, and making all these work on Azure. We tried to get some help from this repository but this is not exactly how we plan to work How should we proceed?
So, we have tried creating a "Basic Azure Node.js Web Application" on Visual Studio and adding loopback to it but when we deploy this, we get "The page cannot be displayed because an internal server error has occurred." error on our page. Also we created an app.js file on the root of the project to make it point like so
var app = require('./server/server.js');
app.start();
and when we look at the logs, we see this error;
Application has thrown an uncaught exception and is terminated:
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:434:25)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
at Function._load (module.js:311:12)
at Module.require (module.js:359:17)
at require (module.js:375:17)
at Object.<anonymous> (D:\home\site\wwwroot\server\server.js:3:16)
at Module._compile (module.js:446:26)
at Object..js (module.js:464:10)
at Module.load (module.js:353:31)
What I think might be the problem is that we can't quite figure out how to make a loopback project in Visual Studio and also how to deploy it so that it runs on our server.

Azure/Node.js Unaught exception: Error: Cannot find module 'ms'

I’ve deployed a small node.js app that runs fine locally in visual studio 2015 to Azure using the Azure publish option (right click – publish – azure profile etc.).
However, when I visit the site all I get on the page is:
The page cannot be displayed because an internal server error has
occurred.
If I connect VS server explorer up to the Azure instance to view the logs, inside logging-errors.txt I can see the following logged lots of times:
Wed Jul 29 2015 12:22:36 GMT+0000 (Coordinated Universal Time): Unaught exception: Error: Cannot find module 'ms'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\home\site\wwwroot\node_modules\debug\debug.js:14:20)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
This is a brand new web app provisioned via the preview portal.
There is no ‘ms’ package defined either in my project dependencies or in the node_modules folder, so I'm assuming this is a Microsoft/Azure specific package required for hosting on Azure.
Have I missed a step in “preparing” my node app for Azure, or is there some other configuarion step I need to perform?
Thanks
It looks like it's having trouble loading modules. Do you have a package.json file that defines your dependencies? The default deployment script that Azure runs when you deploy your site will look for a package.json and perform an npm install --production. That means it will install everything you've defined as in 'dependencies' but not the packages you've defined as 'devDependencies'.
To debug node apps deployed to Azure that won't startup I do:
Open a Powershell on it's scm (https://.scm.azurewebsites.net/DebugConsole/?shell=powershell)
Check your node is set correctly node --version
Attempt to start it and look at errors node .\server.js (or whatever is specified in your package.json > start script.
It would be nice if you could see such a log for when Azure starts the app. However I have not found it.

Error deploying node js app onto Cloudbees ClickStart

I'm trying to deploy a node app onto the Cloudbees ClickStart.
I've create a node js ClickStart (this creates the jenkins job, git repository and application container).
Zipped up my app, then deployed using the Cloudbees SDK.
bees app:deploy -t nodejs -a nodeshort -v nodeshort.zip
When I try to access the URL the application is deployed on I get a 502 Bad Gateway.
Checking the application logs, I see the following printed multiple times:
module.js:340
throw err;
^
Error: Cannot find module '/mnt/e1/genapp-apps/660f9784/main.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:901:3
What has caused this? More importantly, what can I do to resolve it? I've tried trashing the clickstart and recreating, but I get the same outcome each time.
Thanks
The way in which you zip the file is important.
zip -r ../myapp.zip * => WORKS FINE
zip -r myapp.zip myapp/* => DOES NOT WORK
nodejs stack expect a "main.js" script to be present in application rootdir.
You have this nodejs ClickStart working. Maybe you should compare your code with the ClickStart.

Resources