Basic express hello world - node.js

I have the following code in node.js which is very basic express code but I can't get it to work. when I visit localhost/, it gives me text "Cannot GET /". It should display Hello world.
const express = require('express');
const app = express();
app.get('/', function (req, res) {
res.send('Hello World!')
});

do you open port?
The server must open the port.
app.js
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send("Hello World");
});
app.listen(3000, function() {
console.log("server open");
});
If listen method called by app, Prepares to receive the client's response to the port passed as the first argument.
U can execute this code.
node app.js OR supervisor app.js OR forever start app.js ETC

Related

Why is Middleware not working in express js

I am trying to learn middleware in express js. Could anyone help where I am missing? Here is my code
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
app.use("/", function(req, res, next){
console.log("this is my second output");
next();
});
app.get('/', function(req,res){
console.log("this is my first output");
//res.send('Hello World');
});
app.listen(3000, function(){
console.log('Server started on port 3000...');
})
I am getting Server started on port 3000.. when I run on cmd and getting "page is not working" on localhost:3000
Edited
I got
Server started on port 3000...
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
this is my second output
this is my first output
after some time. But localhost:3000 is still not working
The reason you get the "page is not working" message is because your application does not respond to any request it receives.
You'll need to uncomment that res.send('Hello World'); in app.get('/', ...). After that your code works perfectly fine.
Note, however, that in the structure of your code, your middleware app.use(...) is called before you get to the main logic for the route (app.get(...)), contrary to what's indicated by your console.log calls.
var express = require('express');
var bodyParser = require('body-parser');
var path = require('path');
var app = express();
// use this middleware to pass all the requests
app.use("/", function(req, res, next){
console.log("this is my second output");
// move to next middleware
next();
});
//handle all the get requests to localhost:3000 url
app.get('/', function(req,res){
console.log("this is my first output");
// send the response
res.send('Hello World');
// or you can send the response like this
// res.json(JSON.stringify({"success":"true"}));
});
app.listen(3000, function(){
console.log('Server started on port 3000...');
})
send a get request to http://localhost:3000
Mainly, Middlewares execution consists of two things: ->
next()
app.use()
const express = require('express');
const app = express();
const hola = function(req, res, next) {
console.log("are you okay?");
next();
console.log("Yes I'm okay....");
}
app.use(hola)
app.get('/',(req, res) => {
res.send('HEY SUDEEPA WELCOME TO OUR SERVER !!!
})
app.listen(3000,() => {
console.log('Server started at port 3000!!!! ');
})
In the above code if we do not mention the next() method in hola function then after clicking localhost:3000 our will got crashed because it is not getting it's next middleware which is our app.get. So, if we are not mentioning next() then it could be our one of reason for not getting executed Middlewares.

Proxy to other local webserver in node.js

i have following problem in my express application for now. I have different services with build in webserver running. The problem is now i need to access it following:
localhost:8888
The normal server can be accessed over localhost:3000.
Is it possible to create something like a proxy to have something like this.
localhost:3000
localhost:3000/admin/chronograf
localhost:3000/admin/mongo
const express = require('express');
const httpProxy = require('http-proxy');
const app = express();
const proxyOptions = {
}
const apiProxy = httpProxy.createProxyServer(proxyOptions);
const serverOne = 'http://localhost:8888';
app.get('/', function (req, res) {
res.send('Hello World!');
});
app.all("/app1", function(req, res) {
console.log('redirecting to Server1');
apiProxy.web(req, res, {target: serverOne});
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
The Problem is that the page is showing put the links of the resources are not rewritten to the new path. I need a config value or some own Implementation to rewrite the urls.
thanks for helping

NodeJS - create express server on button click event

I'm beginner to Node, I just need to create a express server on a button click event, I did as below but it shouldn't work
script.js
$('#button').click(function(){
$.post('/test');
});
app.js
app.post('/test', function (req, res) {
var express = require('express');
var app = express();
app.get('/test', function (req, res) {
res.send('Hello World');
})
var server = app.listen(3100, function () {
var host = server.address().address
var port = server.address().port
})
});

webpage in nodejs Cannot GET/

I installed nodejs and used npm init then i used npm install express --save. I have made my own app.js to define webserver and make it running on my localhost. Now i get error: Cannot GET/. Bellow is app.js code:
var express = require('express');
var app = express();
//here we add content to webpage .get or .post
app.get('/', function (req, res) {
res.send('Hello World!');
});
//here we setup port
app.listen(1337, function(){
console.log('port 1337');
});

error in code while running in nodejs

var express = require('express'),
app = express.createServer();
app.use(express.logger());
app.get('/', function(req, res){
res.send('Hello World');
});
app.listen();
console.log('Express server started on port %s', app.address().port);
I get the error "unexpected identifier"
what is wrong with the code?
I don't think there's a method createServer in express. This is from their docs:
var express = require('express');
var app = express();
app.get('/', function(req, res){
res.send('hello world');
});
app.listen(3000);
http://expressjs.com/en/starter/hello-world.html
express.createServer() has been deprecated. Please check this question
Nodejs / Express - Launching my app: express.createServer() is deprecated
on how to update the project to work with more recent versions of express.
You didn't declare port anywhere and also you didn't mention the port number also.When you are using app.address().portit does not hold any port value so that's why it returns undefined.Here is my code hope this helps for you.Just save this code with a name I saved it as server.js
var http=require('http');
var express = require('express');
var app = express();
app.set('port', process.env.PORT || 8000);
app.get('/', function(req, res){
res.send('Hello World');
});
var server = http.createServer(app);
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
OUTPUT IN TERMINAL:
C:\Users\nodejs\tasks>node server.js
Express server listening on port 8000
Open a browser and type localhost:8000 then
OUTPUT IN BROWSER:
Hello World

Resources