How do I fix an Internal Server Error?- Google Cloud - node.js

I am following this tutorial on Google Cloud Platform and am finding that the SIMPLE app launches successfully up on port 8080 but when I head over to the browser to view it externally, I get an internal server error. The tutorial can be found here:
https://cloud.google.com/appengine/docs/standard/nodejs/building-app/deploying-web-service
I am using Win8.1 using the GCP cloud shell terminal.
I have also tried updating my npm packages, moving my yaml, nodejs files to the next higher directory as well as deleting the package.json in the next higher directory. Like I said, port 8080 can come up and logged to the terminal, but does not come up in the browser. My package.json is as follows:
{
"name": "express",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"description": ""
}
My app.yaml file is
runtime: nodejs10
And my server.js file is
//this is a test by MP2
// date of use : 2020-0601
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello from App Engine!');
});
// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}...`);
});
Any help would be great since I am new to the google cloud. What is also interesting is that the most current version of express is installed at the next higher folder but not in the current one where the desired JSON is. ( and unable to update it) Thank you.
-MP

It seems that this is because there is no express dependency in package.json:
"dependencies": {
"express": "^4.17.1"
}
I have replicated all steps and it worked. In the tutorial there is 4.16.3 version, I have different, because I have used npm install express -s and it added version automatically to package.json. However it works with 4.16.3 as well on my side.
I hope it will help!

Related

Error when deploying a node.js app on a PLESK ONYX server with Phusion Passenger V. 5.3.5

I want to deploy a small node.js app on a PLESK-ONYX server - on which Phusion_Passenger / 5.3.5 is also running!
The following happens:
This works here:
var express = require ('express');
var bodyParser = require ('body-parser');
That does not work:
import express from 'express';
import bodyParser from 'body-parser';
The following error then occurs:
The Phusion Passenger application server tried to start the web application through a Passenger-internal helper tool called the "wrapper". But Passenger was unable to execute that helper tool because it encountered an internal error.
The stdout/stderr output of the subprocess so far is:
/var/www/vhosts/hosting13.netcup.net/mem3-s3.hxxx-3.de/index.js:2
import express from 'express';
^^^^^^^
How can one solve it?
Where is the problem?
Thank you for your help in advance!
Best regards
Markus
Hello, I have new findings in the meantime ...
my problem seems to be - that the highest-available node.js version in PLESK at the moment is 12.4!
In PLESK - i must start the node.js-App with a server.js-File - like this:
const app = require('./index.mjs');
const http = require('http');
http.createServer(app).listen(process.env.PORT);
My node.js-Version is V.12.4 so i must use esm like this:
require = require("esm")(module/*, options*/)
module.exports = require("./index.js")
How can i combine the 2 files in the right way?
Thanks a lot for your help!
Here is the content of "package.json":
{
"name": "mb-test-1",
"version": "1.0.0",
"description": "",
"main": "index.mjs",
"type": "module",
"scripts": {
"start": "nodemon -r esm index.mjs"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"dotenv":"^8.2.0",
"esm":"^3.2.25",
"express":"^4.17.1",
"mongoose":"^5.9.29",
"nodemon":"^2.0.7"
}
}
But unfortunately I haven't found yet - where in PLESK the start command for node.js can be edited!
Best regards
Markus
for using import from you should change type in package.json to module so add this code to the package.json
"type": "module"
Note: but export file is different between commonjs and module.
by default type is commonjs , check this article
you can also using babel module, update scripts in package.json like this
"scripts": {
"build": "rimraf dist/ && babel ./ --out-dir dist/ --ignore ./node_modules,./.babelrc,./package.json,./npm-debug.log --copy-files",
"start": "npm run build && node dist/app.js"
}
after that for running project
npm start

Rendering html on npm start

I have created one project which has index.html and script.js file. So i have done npm init -y to get package.json. If i explicitly double click the index.html file it is opening it normally and functions as i want but if i do with express as res.sendFile(path.join(__dirname, './index.html')); it is not functioning properly. So i want to write an start script in a such way that it should directly run index.html
i have tried this
router.get('/',function(req,res){
res.sendFile(path.join(__dirname+'/index.html'));
})
But is is not functioning well. It is just rendering raw html in normal function it is asking me for permission to access camera and microphone. But here it asking nothing.
{
"name": "demo",
"version": "1.0.0",
"description": "VideoCall App",
"main": "index.html",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "AkshayK",
"license": "MIT"
}
I want to render index.html when i do npm start.
I have made one application with this same functionality for you. You
can check it here. https://github.com/RajatKaushik/Camera-App
Express is Middleware for handling http requests, you will have to make a GET request from the browser. Once you make the request your express app will match the path and will respond back with the file(index.html) in your case.
So, If you want to deliver the index.html file on the browser, do the following steps:-
Run your node application using node [YOUR_FILE_NAME.js] (node script.js).
If your express server is running on port 4200, then make the request to
localhost:4200/
As that's the path that you have for responding back with the index.html file.
I think the reason why your Camera and Microphone is not working is because the code that initiate that is in some script file that you have include in your HTML file as <script src="index.js"> but you are not delivering that file using express.
You can use the following code snippet to deliver that static files needed.
app.use(express.static('__dirname' + '/public'), () => {
console.log('Request Recived');
});
where the public is the folder in which your index.js file is present.
I have made one application with this same functionality for you. You
can check it here. https://github.com/RajatKaushik/Camera-App

Node failing to deploy due to lack of package.json despite having a valid package file in the zip file

I'm trying to deploy a basic node application but whenever I try to deploy it I get an error saying:
"Failed to find package.json. Node.js may have issues starting. Verify package.json is valid or place code in a file named server.js or app.js."
I have tried re-creating the zip file and restarting the node file
I have tried validating my package.json and it is valid
my app.js is simply:
const server = http.createServer( (req,res) => {
res.writeHead(200, {"Content-Type" : "text/plain"})
res.end("Hello World")
})
server.listen(3000)
console.log("Listening on port 3000")
my package.json file is:
{
"name": "Tutorial",
"version": "1.0.0",
"description": "A simple Hello World",
"main": "app.js",
"author": "kittom",
"license": "ISC"
}
It should allow me to see hello world on port 3000 of the url

Deployment failed in aws beanstalk, getting 502 error

Everything worked in my local, I googled few questions, all have to do with port problem. But since I'm using express, I think I will not have hardcoded port problem, as you can see below is partially the code in
www
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
I have a app.js and below is my package.json
{
"name": "my app",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
..
..
}
}
What I've tried:
rename app.js to main.js
created nodecommand.config file like someone suggested
Create the .zip from the contents, not create a zip from the parent folder.
Hopefully somebody else will find this helpful!

NodeJS - Deploy socket server in Jelastic

I have a socket server that I want to deploy in my Jelastic NodeJS setup. It is made of 2 files, being server.js the main file, the one started/opened by node, and worker.js the file opened as a child process by the server when required (each child handles a socket).
I never deployed a project via Jelastic or any other cloud PaaS/hosting. How should I proceed to do this?
I know that I have to write the package.json, but that's all I know.
So far, this is what I've got:
//package.json
{
"name": "Myserver",
"version": "1.0.0",
"description": "My Server",
"main": "server.js",
"dependencies": {
"microtime": ">=1.2.0",
"microseconds": ">=0.0.2",
"mysql": ">=2.5.4",
"point-in-polygon": ">=0.0.0"
},
"devDependencies": {},
"scripts": {
"start": "node server.js"
},
"author": "Magister"
}
I searched on their docs but all they have is how to deploy a WEB application, but my server is not web-related, so that information is not relevant.
Any suggestions?
Do you have a public IP? Most likely WebSocket app won't work behind the proxy so far

Resources