Unable to load the simple nodejs application on Windows - How to troubleshoot? - node.js

I have written the below code
`use strict`;
const express = require(`express`);
//constansts
const PORT = 7777;
const HOST = `0.0.0.0`;
//App
const app = express();
app.get(`/`,(req,res) => {
res.send(`hello from nodejs`)
});
app.listen(PORT,HOST);
console.log(`Running on http://${HOST}:${PORT}`);
Outcome
This is my package.json
{
"name": "samplenodejsserver",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.2"
}
}
UPDATE
It's working at HTTP ://127.0.0.1:7777
But not at HTTP ://0.0.0.0:7777

The IP 0.0.0.0 is just a wildcard which tells the server to listen at any IP, but you still have to enter a IP which points to your PC.

Related

Nodemon giving an issue

I installed nodemon.
I made changes to my code.
It is stuck at RESTARTING DUE TO CHANGES and doesn't restart the server.
Why is this happening and how to solve it?
>>index.js
const express = require('express');
const app = express();
app.get('', (req, res) => {
res.send('Hello, world!');
})
app.get('./help', (req, res) => {
res.send('Help Page!');
})
const port = 3000;
// const hostName = '127.0.0.1';
app.listen(port, () => {
console.log(`Server is listening on ${port}`);
});
>>package.json
{
"name": "app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon index.js"
},
"author": "Sharjeel",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12"
}
}
The problem is with the latest version of the nodemon. The previous or older version runs completely fine without making any issue i.e. nodemon#2.0.7.

Heroku : Application Error When I try to load the page [Node.js + Express]

index.js
// create an express app
const express = require("express");
const app = express();
// use the express-static middleware
app.use(express.static("public"));
// define the first route
app.get("/", function (req, res) {
res.send("<h1>Hello World!</h1>");
});
// start the server listening for requests
app.listen(process.env.PORT || 3000, () => console.log("Server is running..."));
package.json
{
"name": "SimpleApp",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
},
"engines": {
"node": "10.x",
"npm": "*"
}
}
Procfile
web: echo "I don't want a web process"
service: npm start
I have deployed it in heroku.
And the logs are showing as below
2021-06-22T13:16:11.346614+00:00 app[service.1]: ------------------------------------------------------------------
2021-06-22T13:16:11.371025+00:00 app[service.1]: Server is running...
But i am getting application error when i try to load the page.
It is working fine in local
Your web dyno maybe turned off by default.You need to turn it on inorder to run your heroku app
When declaring the middleware check whether that directory you have set is correct
app.use(express.static("public"));
If not then use __dirname to concatinate the correct directory like so
const public = path.join(__dirname, "../public")
then
app.use(express.static(public));

Express app deployed successfully on Azure but still nothing work

Azure deployed this app with successfully flag, but homepage response with 500 status
index.js:
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("Hello!");
});
app.listen(3000, () => console.log("Hwllo!"));
package.json:
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
Have you tried setting the variable process.env.PORT for handling the named pipe port in Azure App Service. See Listen additional port Microsoft Azure Nodejs.
If it does not solve your issue, try to go through KUDU console and look for the particular erros.

SyntaxError, cannot use import statement outside a module, occurs even with "type":"module" in package.json

I am trying to use ES6 Modules to import Express. Although I added "type":"module" in my package.json. The error, SyntaxError: Cannot use import statement outside a module, still occurs. I am expecting an answer which does not require to convert into a .ejs extension since wanting to know what's wrong in my code rather than taking an alternative. Note: package.json & server.js are in the same directory.
package.json
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "server.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"mongoose": "^5.10.3",
"nodemon": "^2.0.4"
}
}
Server.js
import express from 'express';
const app = express()
const port = process.env.PORT || 9000
app.get('/', (req, res) => {
res.status(200).send('hello world');
})
app.listen(port, () => {
console.log(`Listening on localhost: ${port}`)
});

Heroku error module not found 'json-server'

I want to deploy my server on Heroku, but on Heroku build it says this:
The app works fine locally, but somehow it crashes on the Heroku server.
This is the file structure
This is the code for server.js
const jsonServer = require('json-server');
const server = jsonServer.create();
const router = jsonServer.router('data.json');
const port = process.env.PORT || 4000;
server.use(router);
server.listen(port);
This is package.json
{
"name": "server",
"version": "1.0.0",
"description": "json-server",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "npm install && node server.js"
},
"author": "japsuchiha",
"license": "ISC",
"dependencies": {
"core-util-is": "^1.0.2"
},
"devDependencies": {
"json-server": "^0.15.1"
}
}
Pure guess, but I don't think heroku is installing json-server since it's listed under devDependencies

Resources