Request Hangs in express.js - node.js

I have created a brand new express API application and for some reason when I run it (the command is node src/index.js) both requests hang and there is never a response. I have been able to nail it down to the app.use(express.json) line as the culprit. When it is commented out the requests seem to work, but then I do not get access to request body.
const app = express();
app.use(express.json);
app.post("/api/user/register", (req, res) => {
res.send("Ok");
});
app.listen(3000, () => {
console.log("API is up and running!");
});
The contents of my package.json file is as follows:
"name": "api",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node src/index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
I am currently running node version 13.2.0. Any suggestions on what to try next? (I'm new to node so any help would be appreciated)

It's supposed to be app.use(express.json()); not app.use(express.json);

Related

Nodemon gives UnhandledPromiseRejectionWarning upon saving file

I'm using Windows and I've just installed nodemon 2.0.12 (added to path). Whenever I run a basic application it works it should until I save a file, then I receive the following error
My project is a basic express app:
const express = require('express')
const app = express()
const PORT = 3000
app.use("/", (req, res) => res.send('test'))
app.listen(PORT, function() {
console.log('Server started on port 3000')
})
My package.json is as follows
{
"name": "express.1",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"dev": "nodemon app.j"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.12"
}
}
What might be the reason for this?
Downgrading Nodemon to v2.0.7 seems to work. I don't understand why it broke in the first place.

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));

Push rejected, failed to compile Node.js app on heroku App

I have added node_modules in .gitignore that's not issue!
How to solve this errors because in engines I have specified version of both.
Here is Code :
{
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node":"12.6.2",
"npm":"6.14.4"
},
"scripts": {
"start": "node index.js"
},
"author": "smeet_kothari",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
and this is my index.js file
And Code of index.js file.
const app = express();
app.get('/', (req,res) => {
res.send({hi:'there'});
});
const PORT = process.env.PORT || 5000;
app.listen(PORT);
G:\01) Web Development\10) nodejs project\[FreeCourseSite.com] Udemy - Node with React Fullstack Web Development\MailFeed\server>heroku -v
heroku/7.47.7 win32-x64 node-v12.16.2
Procfile has been added too!
Thanks for solution in advance!
Seems like you have not defined Procfile for the project try it first if still not solved comment the total error appearing in the command line

Azure Web App getting "You do not have permission to view this directory or page." when launch app

I'm using nods js with express. I've tested it on my local machine and the page pulls up easy enough.
start server with
node index.js
from my understanding I don't need to explicitly define the web.config so I haven't. I
here are my files
index.js
const http = require('http');
const fs = require('fs');
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World')
})
app.get('/api/courses', (req, res) => {
res.send([1,2,3])
})
app.listen(3000, () => console.log('listening'))
package.json
{
"name": "geoscavenge",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://geoscavenge.scm.azurewebsites.net:443/geoscavenge.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.17.1"
}
}
This problem is generally caused by the lack of web.config.
Solution:
Deploy the webapp in the Linux environment.
Still use the windows environment, manually add the web.config file, you can refer to https://github.com/Azure-Samples/nodejs-docs-hello-world/blob/master/web.config
The easiest way is to use git for deployment. In git deployment, git will automatically create a web.config file.

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}`)
});

Resources