Deploying Loopback Node.js server using VSTS and Azure deployment - node.js

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.

Related

Running Next.js App on EC2 Instance with IIS

I have an AWS EC2 instance for my Next.js website. If I open the terminal on my Next.js app and run next start it runs fine and I can visit the website. However, I then need to always keep the terminal open on the instance for the website to work.
My question is, can I use IIS to keep a Next.js website running with next start? I don't want to create a full node backend to serve static html pages because it will mess up some of the dynamic aspects of the website. So is there a way to run a Next.js app by itself without running next export for the static pages?
UPDATE
I attempted to run the Next.js app with PM2 instead. When I run npm start in the terminal it works fine. However, when I run pm2 start npm -- start (or any variation of that), it stops the server and logs this error multiple times:
SyntaxError: Unexpected token ':'
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1031:15)
at Module._compile (node:internal/modules/cjs/loader:1065:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Object.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\pm2\lib\ProcessContainerFork.js:33:23)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
C:\PROGRAM FILES\NODEJS\NPM.CMD:1
:: Created by npm, please don't edit manually.
You can use forever to run your process in the backend, the below example is how to implement it with iis
https://www.technoapple.com/blog/post/run-node-as-background-by-using-forever

Deployement Nuxt.js ssr on passenger : SyntaxError: Cannot use import statement outside a module

I'm deploying my nuxt app on planethoster who use Passenger to run Node.js Application. I build the app by using ">npm run build" and deployed the content off .nuxt folder on the server, with "server.js" as entrypoint. but I ran on the following error:
/home/scalcmjx/test.scalomhouseplans.com/server.js:1
import Vue from 'vue'
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at Module.require (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:80:25)
at require (internal/modules/cjs/helpers.js:74:18)
at loadApplication (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:243:2)
at setupEnvironment (/opt/cpanel/ea-ruby27/root/usr/share/passenger/helper-scripts/node-loader.js:214:2)
Please Someone know how to manage it?
What is your node.js version? Try using the current LTS: 14.17.0
If I'm not mistaken, the import syntax is officially supported since v14, so this should maybe fix one issue.
Also, you need to deploy dist and not .nuxt, the latter is used for caching purposes.
I'm not sure of what is the process for this specific host, but you should probably get some decent ideas from the deployment section of the official documentation: https://nuxtjs.org/docs/2.x/deployment/deploying-to-21yunbox
There are a lot of hosting solutions there, one is probably looking like Planethoster.

nestjs to azure windows app service - web.config settings

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"

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.

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.

Resources