gcloud Nodejs http 500 error cannot find server.js in workspace [more detailed repost] - node.js

I am new to google cloud and so I deployed a Nodejs app using the tutorial. Everything worked just fine, here is my file structure (I didn't make the my-nodejs-service folder):
app.yaml
server.js
package.json
I deployed it, everything was fine. So then I tried to make it look like this:
mySite/
server.js
package.json
app.yaml
public/
site contents
I just forked a github repo, deployed it, and it didn't work. By that I mean the site didn't load! I checked the logs, and here was what appeared:
Error: Cannot find module '/workspace/server.js'
at Function.Module._resolveFilename (loader.js:880)
at Function.Module._load (loader.js:725)
at Function.executeUserEntryPoint (internal/modules/run_main.js:72)
"GET / HTTP/1.1" 500
listening on port 3000
It said it couldn't find the server.js file. Was it because I moved the code into the folder? I deployed it on the current working directory being the folder mySite.
package.json (Im only including the scripts part, it other stuff such as the dependencies):
{
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
}
}
Other things I tried:
deploying with app.yaml at the end of the deployment command
Using path.join()
Deploying multiple times
Other info:
I checked the logs and I got this:
"GET / HTTP/1.1" 500 - - "Web browser agent info in here"
Another showed:
i This request caused a new process to be started for your application, and thus caused your application code to be loaded for the first time. This request may thus take longer and use more CPU than a typical request for your application.
And another log said:
!! Process was terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the PORT environment variable. (Error code 123)

App Engine uses a set of conventions in order to be able to deploy and run code successfully. As you've seen, you can't necessarily take an arbitrary web server solution and deploy it successfully.
Please include more details about specific steps you take. For example, you say "I .. deployed it". It would be useful for you to include the command that you used and any immediate errors that were thrown to provide readers with more context when helping answer your question. It's unclear to me from which directory you attempted to deploy the mySite example but I assume from within mySite.
The first error Cannot find module '/workspace/server.js' results because App Engine builds your code using Cloud Build and Cloud Build (by default) puts your code into a /workspace directory on the build VM. It's not very user-friendly, but it was unable to find server.js and, because your package.json specifies node server.js, it needs it.
Another statement that gives me pause is listening on port 3000. I'm not very familiar with the Node.JS runtime but generally App Engine expects web server's to either listen on port :8080 or provide an environment variable that specifies the port, In your tutorial, this is explained: "process.env.PORT is not set, port 8080". That's probably (!?) another issue.
Lastly, you may (I think you probably don't) need to explicitly reference the public subdirectory (unsure) in order to ensure that it's available at runtime.

Related

How to use Node -r flag inside npm scripts on Azure App Service

I am running a node site on an Azure App Service but it won't start with the following line in my package.json scripts.
"scripts": {
...
"serve": "node -r dotenv-azure/config dist/server.js",
...
},
The -r flag is required to preload environment variables using dotenv-azure, as per their own instructions... https://www.npmjs.com/package/dotenv-azure
it works absolutely fine on localhost (Windows), it loads the config before starting the server. On Azure App service (windows) however it fails to start the server and I get a 503 error after starting it.
I have tried switching on application logging but because it can't even start the server I get nothing in the logs.
After add httpplatformhandler in web.config file, it works for me. You can download my sample code from github. You will find my web.config file.
Test Steps:
1. Create a sample code.
2. Create .env file.
3. Test result in local.
4. Deploy by git.
After deployed, it also has some error, we should add web.config to solve it.
After add web.config file ( With httpplatformhandler):

App Dynamics for Angular 2 App in IIS - Node.js Agent Installation and Configuration

I am trying to assist in setting up AppDynamics with an Angular 2 app that is hosted in IIS. The app is already up and running. There is a part I am having trouble on, the instructions for that part say say:
1) From the root directory of your Node.js application, run this command:
npm install appdynamics#4.3.5
For every Node.js application you are instrumenting, insert the following call in the application source code at the first line of the main module (such as the server.js file), before any other require statements:
require("appdynamics").profile({
controllerHostName: '<controller host name>',
controllerPort: <controller port number>,
controllerSslEnabled: false, // Set to true if controllerPort is SSL
accountName: '<AppDynamics_account_name>',
accountAccessKey: '<AppDynamics_account_key>',
applicationName: 'your_app_name',
tierName: 'choose_a_tier_name',
nodeName: 'choose_a_node_name'
});
2) Restart you application
I did step 1 locally in the console, but I don't know what to do for step 2. If I add that script to the page I get "The Reference error: require is not defined".
I learned that that function is not meant to run on the browser. It's meant to be run server-side, but I do not see node js or any server.js files on our dev web server.
Does anyone have any suggestions on where to put that snippet. Will it even work with the current setup?
It turns out the code I was given was completely wrong for angular 2 implementation. The code they gave me is for running on the web server's side with node js. Since angular 2 is an SPA that runs on the browser, it would never work.
I did some research and found this example application that I added a few tweaks to: https://github.com/derrekyoung/appd-sampleapp-angular2

app.yaml in different directory than main.js?

I have a Node.JS application that I want to deploy on AppEngine. At the moment I am getting following error:
Beginning deployment of service [default]...
Building and pushing image for service [default]
ERROR: node.js checker: Neither "start" in the "scripts" section of "package.json" nor the "server.js" file were found.
ERROR: (gcloud.app.deploy) Your application does not satisfy all of the requirements for a runtime of type [nodejs]. Please correct the errors and try again.
This makes sense as this is the folder structure:
client
server
main.js
app.yaml
As you can see I have a separate folder for my client code and server code.
If I put the app.yaml file inside the server folder, the deployment works but when I try to access the app it cannot find the client files as they were not part of the deployment.
Is there anyway to configure this in the app.yaml or do I need to refactor my app?
actually app engine required package.json in your root directory ... and in which put script of start.. like start:node server/main.js

Vue.js Webpack Template in a Docker Container: How do I add Webpack-Dev-Server --watch-poll flag?

I am running the webpack / webpack-dev-server portion of the base Vue.js Webpack template (https://github.com/vuejs-templates/webpack/) inside of a docker container I created. The container also contains the vue CLI in order to create new projects (you can get my container here if you want: https://hub.docker.com/r/ncevl/webpack-vue/).
Hot-reload does not work after moving from the webpack-simple template to this one.
Everything was working using the Webpack-Simple template which you can clone / see over here: https://github.com/vuejs-templates/webpack-simple
I was able to get the simple template running (with hot-reload working as intended) with the following webpack-development-server launch command:
webpack-dev-server --hot --inline --progress --host 0.0.0.0 --watch-poll
That said the full (not simple) version of the webpack template does not appear to use a webpack-dev-server launch command and instead appears to use additional middleware as referenced in build/dev-server.js (https://github.com/vuejs-templates/webpack/blob/master/template/build/dev-server.js) and the webpack dev config.
Since the --watch-poll was the key to getting the WDS hot-reload functionality to work within a docker container in the last project, my thinking is that I need to do something similar with the webpack-hot-middleware but I dont see anything in their docs (over here: https://github.com/glenjamin/webpack-hot-middleware) that talks about changing to a polling based approach.
I am not 100% sure the polling flag will do the trick since I can see the container recompile my source when I make a change. I can also see the change in my browser if I refresh it manually.
Whats stranger still is if I inspect my page in browser within chrome dev tools, and then head over to network / XHR I can see that the browser actually does receive information from the webpack-dev-server, but visually it does not update.
Give the above I assume websockets (or socket.io which I think is used) are working and communicating between the browser and the WDS so maybe this is a browser caching issue of some sort?
I checked in my console and found this so it is looking like a header issue:
For reference the text error from that image (to make it easier for anyone having the same issue to find this post) is:
EventSource cannot load http://__webpack_hmr/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://0.0.0.0:8080' is therefore not allowed access.
Again the Hot-Reload / Hot Module Reload was working with this identical container setup when using the webpack-simple Vue.js template.
I am wondering if anyone has run into anything similar or has any ideas on how to add the polling option . I guess my alternative would be roll back to a more basic webpack config and rebuild that portion of things to use the traditional webpack-dev-server / webpack config but give the above I am not sure that is going to fix it.
I am adding this as a separate answer since it more specifically answers the question in the title, while my other answer more specifically explains what solved my actual problem.
The vue.js webpack template project (which can either be init'd from the Vue CLI or pulled from its repo over here: https://github.com/vuejs-templates/webpack) separates its config files into several different directories.
I am posting this answer so that anyone who runs into the need to add polling to their project will be able to understand how / where to do that.
The base project structure for a Vue.js webpack template project looks like this:
The files that you care about if you are messing with trying to get hot module reload working are related to creating your server primarily with webpack-dev-middleware. The most important files related to that are highlighted here:
Basically if you want to add the polling code to the webpack-dev-middleware server you need to be in the /build/dev-server.js file on lines 20 to 24 that look like this:
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: true
})
To add polling you would add it just before or after quiet: true. As a side note, if you are having trouble with HMR I would change "quiet:true" to queit false to get a more verbose read out of whats going on from webpack-dev-middleware. I have included verbose and polling modifications to the above code here:
var devMiddleware = require('webpack-dev-middleware')(compiler, {
publicPath: webpackConfig.output.publicPath,
quiet: false, //Changed to for additional verbosity
watchOptions: { //Add Polling
aggregateTimeout: 300,
poll: 1000
}
})
My other answer is in regards to what ended up solving my problem, not necessarily how to actually add polling (which might be necessary for someone else but did not end up being needed to make my dockerized setup work).
It should also be noted that sometimes when HMR (webpack hot module reload) is not detecting changes it is due to the fact that webpack-hot-middleware or webpack-dev-middleware is running into an issue whereby some invisible characters are / were added to the name of the base project directory (probably by someone building the base Vue project) and therefore webpack on certain OSes is not able to see the changes.
If that happens to you and you are on OSx or running webpack inside of a docker container and you can't get HMR to detect changes, try to rename your vue-webpack project directory and it should work.
Ok. So I can't really take credit for this one since it was actually answered by Discuss user Cristian Pallarés over here: http://webpack.github.io/docs/webpack-dev-server.html#combining-with-an-existing-server
Christian says:
I was just trying the same. I just use "php artisan serve" on localhost:8000, and Webpack Dev Server on localhost:3000. You should make this:
set your webpack config "output.publicPath" as "http://localhost:3000/static/" instead of "/static/"
make your php application load this:
The key is the output.publicPath being absolute. Now, you should run "php artisan serve" and launch your webpack dev server too (in my case I use gulp).
Basically I took that and dug through the Vue.js Webpack Template files to locate the config file where webpack was looking for the public path. the public path setting ended up being in the index.js file located in the /config directory of the template.
I changed my code to look like this:
assetsSubDirectory: 'http://localhost:8080/static/', //!!Changed from /static/
assetsPublicPath: 'http://localhost:8080/', //!!Changed from /
As opposed to the previous setting which DID NOT WORK and looked like this:
assetsSubDirectory: '/static/',
assetsPublicPath: '/',
After that I was able to see my changes hot reload while running the vue.js Webpack template from within my docker container.

How to correct a Bluemix Node.js app that can't accept connections

I created a new Node.js app on Bluemix this morning and downloaded the boilerplate code. I worked on it locally and then pushed it up. On Bluemix, it refuses to start. The error according to the logs is:
Instance (index 0) failed to start accepting connections
So I Googled for that, in every case where I found the result, the answer was that my application was trying to use a specific port instead of letting Bluemix set it.
Ok, but I'm setting the host/port with the exact code the boilerplate uses:
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
So if this is incorrect, it means the code Bluemix told me to download itself is incorrect as well, and I can't imagine that is the issue.
To identify whether cfenv is at fault, I've tested that piece of code with a number of more complex Node.js apps I have, and they work perfectly on Bluemix.
That message can also come when an application you've deployed to Bluemix fails to start at all. Here's a few things you can do to troubleshoot your Node.js application on Bluemix.
Tail logs in another terminal while pushing with "cf logs
". Inspect logs after the failure to see if something
failed during the staging process.
Check that your start command in one of two recommended places, scripts.start in package.json or in a Procfile with web: node <start-script>.
Check that your application works locally. First (optional), create a .cfignore file with "/node_modules" in it, so that when you push the app to Bluemix, CF CLI doesn't push your entire folder of node_modules (as they will be installed dynamically). Next, wipe out your node_modules directory and do an npm install --production followed by npm start (or your custom start command). This is what Bluemix does when trying to start your application, so you should double check that it works locally.
Finally, try bumping up your memory, although this is very unlikely that this is why your application fails to start.

Resources