nodejs + express strange things on different ports - node.js

Can someone tell me what is going on?
I have an aplication:
var express = require('express')
, http = require('http')
, app = express()
, port = 3000
, mw = require('./lib/middlewareView')
app.use(mw());
app.get('/', function (req, res, next) {
res.send("hello");
});
app.listen(port, function() {
console.log("Listening on " + port);
});
and /lib/middlewareView.js :
module.exports = function middlewareView(){
return function middlewareView(req, res, next) {
console.log("middleware run");
next();
};
};
When I'm using port 3000 and open http://localhost:3000/ everything seems to be fine.
console output:
Listening on 3000
middleware run
hello
But if I switch to port 5000, console prints this:
Listening on 5000
middleware run
hello
middleware run
middleware run
So the middleware runs 3 times during one request, right?
Is it normal?

Its probably to do with favicon.ico - this happens when retrieving the icon for your site, check logs to make sure...

Related

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?

I want to check the server.When I tried to run the code in console using:Node server.js, it didn't run the code. Any ideas why?
This is my code where I tried to check the server
const express = require("express");
const app = express();
app.get("/", function(request, response){
console.log(request);
})
app.listen(3000, function () {
console.log("Server started on port 3000");
});
and here I try to run the code in console, and it simply doesn't do anything: it shows me the location and that's all
Helens-MacBook-Pro:my-express-server helenmyrlen$ node server.js
Helens-MacBook-Pro:my-express-server helenmyrlen$
I am also new to NodeJS so I can't find where you are doing are but you can try with the below script. It works.
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Run the app with the following command:
$ node app.js
Then, load http://localhost:3000/ in a browser to see the output.

Error occurred while trying to proxy request [...] from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET)

I'm working on my first project using react and node and have been stuck on this problem for a while. I keep getting this error when trying to connect to my site using the ip address, but if I just do localhost:3000 it works perfectly. I want to be able to connect via the IP address so I can test it across devices. Here is the full error:
[HPM] Error occurred while trying to proxy request /socket.io/?EIO=3&transport=polling&t=N4EqtUl&sid=kz_I098ZM2h1Z6WZAAAI from 192.168.0.4:3000 to http://192.168.0.4:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)
I checkout out this post and implemented setupProxy.js like the second answer suggested, but it still isn't working.
Here is my node server (index.js):
const express = require('express');
const app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const path = require('path')
const port = process.env.PORT || 5000;
app.use(express.static(path.join(__dirname, 'client/build')));
io.on('connection', function(socket) {
console.log('a user connected');
});
// Anything that doesn't match the above, send back index.html
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/client/build/index.html'))
})
http.listen(port || 5000, function () {
console.log('listening on', port);
});

localhost port is not working although the code is responding in console

I have done everything correct. it is also working in console.log but not in browser in localport. please help
var express = require("express");
var app = express();
app.get("/", function(req, res){
res.send("Hi there, welcome to my assingment");
})
app.get("/speak/goat", function(req, res){
res.send('The goat says "Oink"');
})
app.get("/speak/dog", function(req, res){
res.send('The dog says "Bow"');
})
app.get("/speak/goat", function(req, res){
res.send('The goat says "Oink"');
})
app.get("/repeat/:anything/:value", function(req, res){
var anyThing = req.params.anything;
var value = req.params.number.value;
for(i = 0 ; i >= value ;i++ ){
res.send(anyThing);
}
})
var PORT = process.env.PORT || 6000;
app.listen(PORT, function(){
console.log("hello " + PORT);
});
Looks like you are trying to open the localhost:6000 on chrome which is resulting into "ERR_UNSAFE_PORT" - Its because of using a NON STANDARD PORT.
You can use different port example 3000 or 4000 or even 6001.
- Internet explorer works on port 6000 :-)
find more here https://douglastarr.com/how-to-allow-unsafe-ports-in-chrome

appmetrics(https://github.com/RuntimeTools/appmetrics/issues) request and http event not working

Hi Please see below sample code.
const express = require('express')
const app = express()
var port = process.env.PORT || 3000;
var appmetrics = require('appmetrics');
var monitor = appmetrics.monitor();
appmetrics.enable('http');
appmetrics.enable('request');
monitor.on('request', function (request) {
console.log('request', request);
});
monitor.on('http', function (http) {
console.log('http', http);
});
app.get('/', function (req, res) {
res.send('Hello World!')
})
app.listen(port, function () {
console.log('Example app listening on port 3000!')
})
'
Whenever I fire localhost:3000 from browser. I get 'Hello World!' reply but logs doesn't show any request or http event. Can somebody help me to point out issue.
I am using latest appmetrics version.
Thanks in Advance.
Have you tried putting line 4 (the appmetrics require line) at the top of the code? If it works, please close the issue you raised https://github.com/RuntimeTools/appmetrics/issues/452

How to setup and teardown express with node?

I have a very basic node script that starts up, serving up an html page and finally closes the app (kills the node process ideally). Everything works great except the last line that invokes the close method to tear it down.
var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
app.listen(8090);
console.log("started...");
app.close();
The error I get when I run this w/ node 0.8+ is
app.close();
^
TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'close'
Anyone know how I can more safely close the express app down?
You were so close:
var express = require('express')
, http = require('http')
, app = express()
, server = http.createServer(app);
app.get('/', function(req, res) {
res.sendfile(__dirname + '/index.html');
});
var listener = app.listen(8090);
console.log("started...");
listener.close();
So far the best approach I've found it to kill the node process. seems to get the job done (although for purity I'd like to just close express)
process.exit(code=0)
process.on('SIGTERM', function () {
app.close();
});
Try that

Resources