Terminal stuck at white starting nodejs - javascript

I'm trying to setup a nodejs based server, but unfortunately, when ever I run it via terminal, node app.js it returns console log thing what ever I declare in app.js file, and after that, it stuck, I can't not use it to process more tasks.
var http = require('http');
var express = require("express");
var app = express();
app.get('/files/:hash/:title', function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(req.params));
console.log('Done');
});
app.listen(8080);
console.log('Server running');
and terminal's response
root#mrboota13:/var/Node# node app.js
Server running
Done
Done
Done
Done
Done
Done
Done

That's because the Node process is still running, waiting for new HTTP requests to arrive.
To stop the process, press Ctrl+C

Related

Restart express server every time a request is made to an endpoint

basically I would like to 'restart' it every time a client sends a request to /reset, how can I do that? any help is extremely valuable, I don't know how to approach this yet
As #jfriend00 mentioned, You can use pm2 like applications to monitor the process. Below is simple steps that can be followed.
Node Js code: Exit the process on route
const express = require("express");
const app = express();
app.get("/", (_, res) => res.send("hello"));
app.get("/restart", (_, res) => {
process.exit(0);
});
app.listen(8080, () => console.log("Server is running on :8080"))
Run server using pm2 in watch mode:
./node_modules/.bin/pm2 start app.js --watch
He also mentioned that you should not use this in the production environment. I support that statement. You should not use this approach in prod environment.

I'm having trouble executing a simple JavaScript code on the server using node JS

I'm a beginner with Node.js.
I have just installed node JS, and I'm trying to create and execute code through a local server (for practice purposes). But it can't be possible to execute the code using localhost:8080 on a web browser nor through the cmd (I'm on windows 7).
Below you can see what I've been trying so far...
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(8080);
The browser just says that the site can't be reached and the cmd shows nothing after executing the command. So what would be the problem.
It would be a good approach to get the PORT environment variable and to have the server listen on that port, instead of one that is hard-coded.
In node you can get this with: var port = process.env.PORT || 8080
This is saying that if the PORT environment variable is set use that OR use 8080 if it is not set.
Your new code would be:
var http = require('http');
var port = process.env.PORT || 8080;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World! i did it');
}).listen(port);
If you are using Windows CMD, first type 'node'. Now you will be coding directly in node.js. Now, copy and paste your code, and hit enter. Now go, and try the localhost:8080 on your browser.
If you still have the issue, try the same with a different port.
I tested your code on my machine and works perfectly fine.

Get Node js Server Url

I am new to node.js. I just installed node.js on my production server it was installed correctly and node.js is running on my putty cli the problem is I cannot access it on my browser. I think my problem is because I installed created my server on a subdirectory inside a subdomain e.g. subdomain.example.com here is my code
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(3000); //the server object listens on port 8080
Now I am trying to access the node.js server by going to this address subdomain.example.com:3000 but I am getting no results. Please help Thanks!

Express Does app.js get ran on every request?

I am building an express server with pretty standard stuff. I've been unable to get express.router() to execute my routes correctly, which has caused me to dig deeper into what is actually happening when a page is requested from a server running an express app.
console.log('App.JS has ran!');
var http = require('http');
var express = require('express');
var app = express();
var server = http.createServer(app);
var mongoose = require('mongoose');
mongoose.connect('mongodb://52.27.161.16');
mongoose.connection.on("connect",function(err) {
if (err) throw err;
console.log('hello found!');
});
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var router = express.Router();
router.get('/hi'), function (req, res) {
res.send('/hi route response');
};
router.get('/', function(req, res) {
res.send('default route reached');
});
app.use('*', router);
server.listen(config.server.listenPort);
Pretty standard stuff—but for some reason whenever I navigate to localhost:port/hi I am only getting the res from the / path, i.e. router.get('/', function{} (res.send('default route reached'));
So I've become more interested in what's happening behind the scenes. I've noticed that the server only logs to the terminal the output not related to the bodyParser on the first request. I.e., the console.log at the top of the file only gets ran when the application is started, and never after, though bodyParser correctly logs requests for each request instance.
What's going on exactly when a request is made to the server? Is the app object and route cached and being served? Why is app.js not being re-evaluated on each request? Is only the router object responsible for sending requests over?
It would be helpful to know this, to figure out why my router is not responding with the correct route.
Thanks a bunch!
The app object is the express application that you are creating. It is basically a wrapper for the express module which includes all the express functionalities. It basically reduces the code that you requires to handle requests made to the server, rendering the HTML views, registering a template engine etc. This app object is passed to the server object that you are creating and the server continuously listens to requests in the port that you have configured. So, when the server is running, the app object is initiated only once and the requests to the server are handled by the node event loop.

nodejs web server not responding

i am trying to setup a web server using nodejs. The following code below is directly from the nodejs.org website just configured with my server credentials.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(5000, '10.0.1.51');
console.log('Server running at http://10.0.1.51:5000/');
but when i go to 10.0.1.51:5000 nothing is found i don't even get an error in my console.
also the book i am learning from provided me with this
var connect = require('connect');
connect.createServer(
connect.static("../angularjs")
).listen(5000);
and that still doesn't work. I'm not sure on where to look to resolve this issue, thanks.

Categories