Express.js showing ERR_CONNECTION_REFUSED - node.js

I have installed node(v4.1.2) and express(4.13.3)
Node Server code:
var express = require('express');
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
After running the node file and upon calling http://localhost:3000/ gives me ERR_CONNECTION_REFUSED.

Have you run in powershell at server directory?
node app.js

You are listening on port 3000.
So Try http://localhost:3000

At first install Node.js body parsing middleware.
Installation: npm install body-parser
Then add the following lines:
var bodyParser = require('body-parser');
app.use(bodyParser.json());
I think your problem will be solved. For more details, visit -
http://expressjs.com/en/resources/middleware/body-parser.html

Related

express and http server usage togather

Gone through code as below couldnt understand the working of code 1...what is the difference between the two codes below
**
What is the point of using http and express togather in code 1?
Code1
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res)
{
res.sendFile(__dirname+'/index.html');
});
http.listen(3000, function()
{
console.log('listening on *:3000');
});
The same thing can be done as
Code2
var express=require('express');
var app=express();
var socket=require('socket.io');
app.get('/',function(req,res){
res.sendFile(__dirname+'/index.html');
}).listen(8080);
console.log("Listening to port 8080");
You're asking about the difference of expressjs own server and http server. They are different in many ways.
Solved here
The app object conventionally denotes the Express application which is created by top level express() function exported by the Express module.
http.listen(): Starts the HTTP server listening for connection
In the second case it works app.listen() which binds and listens for connection on the specified port and it identical to http.listen()

Express launching Angular application

I have an express server setup online which loads multiple ports and those ports are setup on subdomains for example. port 9000 loads the main domain.com port 8000 loads the main application at "app.domain.com" port 1000 loads "signup.domain.com" and the build version of the app is on port 8500 "build.domain.com".
The application is an Angular application however when I go to load the Angular app it loads on port 4200 or it says 8500 is in use. So currently I am loading that in express like so:
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
So my question is in Express how can I instead of writing sendfile command make it launch the angular app in that location on port 8500 so my subdomain names will work. The reason I'm asking this is because right now all it does is load the index file but angular or the app isn't running so i just see source code that says app-root and a blank white page.
Thank you in advance.
Robert
--- Update. I've decided to post the entire Express file. My issue is trying to load a angular app on port 8500 from the subfolder upon booting of express. Here is the full server.js code:
// server.js
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
cors = require('cors'),
mongoose = require('mongoose'),
config = require('../config/DB');
mongoose.Promise = global.Promise;
mongoose.connect(config.DB).then(
() => {console.log('Database is connected') },
err => { console.log('Can not connect to the database'+ err)}
);
// Main Website
var web = express();
web.get('/', function (req, res){
res.sendFile('/web/index.html', { root: '.' })
});
var port = 9000;
web.listen(port);
console.log('Web Listening on port', port);
// Main Application
var app = express();
app.get('/', function (req, res){
res.sendFile('/app/index.html', { root: '.' })
});
var port = 8000;
app.listen(port);
console.log('Main App Listening on port', port);
// Build Application - In Development
var appbuild = express();
appbuild.get('/', function (req, res){
res.sendFile('/app/build/myapp/src/index.html', { root: '.' })
});
var port = 8500;
appbuild.listen(port);
console.log('Build App Listening on port', port);
// Sign up Portal
var sign = express();
sign.get('/', function (req, res){
res.sendFile('/signup/index.html', { root: '.' })
});
var port = 10000;
sign.listen(port);
console.log('Sign Up Portal Listening on port', port);
Refer to this link https://malcoded.com/posts/angular-backend-express
Update your code to the following:
const express = require('express');
const app = express();
app.listen(8500, () => {
console.log('Server started!');
});
You need to build the angular app if your angular version not 1.x
ng build
Also, I think this question is similar to your question:
Not able to view Angular app via express/heroku?

How can I make PhoneGap run with express using the same port in PhoneGap desktop application?

I am facing a problem connecting to a PhoneGap desktop application in my iPad.
I am using express to query to a MongoDB database which uses port 3000. However, PhoneGap desktop application also uses port 3000. If I change the port of my PhoneGap desktop application, I would be able to connect to the PhoneGap desktop application in my iPad. But, the query to MongoDB will not work.
How do I run both at the same time (being able to use in iPad)?
App.js:
var express = require('express');
var http = require('http');
var path = require('path');
var mongoose = require('mongoose');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/view');
app.set('view engine', 'jade');
app.use(express.bodyParser()) ;
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
var server = http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
I tried using this method, the result is still the same.
var phonegap = require('connect-phonegap'),
express = require('express'),
app = express();
app.use(phonegap());
app.listen(3000);
please try the following code piece:
var server = app.listen(3000,'::1', function () {
var host = server.address().address;
var port = server.address().port;
console.log('running at http://' + host + ':' + port)
});
And try accessing the node process using the following URL:
http://[::1]:3000/
Phonegap should be accessible on the following URL:
http://localhost:3000
Let me know if this helps.

Node.js server with http get json

I want to create a simple Node.js server to do the following :
With my application I just do the command http.get(Node.Js_Server_address/json) to get the json file data stored on my server.
Could please help me with a tutorial? Any help would be appreciated!
This is very simple example of node.js server:
var app = require('./app');
var http = require('http');
var server = http.createServer(app);
server.listen(8080, function() {
console.log("listening to: http://127.0.0.1:8080");
});
// routing
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
there is a nice tutorial here and here ...
you can use npm to install node.js and all the packages that you need for it.
hope it helps.
There are lots of examples on this topic, i think you should make some googling before next time.
You can create a REST server via express module of nodeJs. In your server folder use npm install express to download express module. You can get more information about express from here. After that create a server.js file in your server folder.In server.js
var express = require('express');
var app = express();
var PORT = 8080;
/* req stands for request, res stands for response */
app.get('/json',function(req,res){
res.json(yourData);
})
app.listen(PORT,function(){
console.log('Express is listening port:' + PORT + '!');
})
So this should do the work. Let me know if this helps you.

app.set('port' , process.env.port || 3000) typeerror object #<object> has no method 'set' at object.<anonymous>

I am using Express 4.2.0 and node.js 0.10.12.
The weird thing is that I created a project in C\program files\node\nodetest and when I did npm start I got no errors.
Now I created a project in C\program files\node\secondtest and when I do npm start I get
app.set('port' , process.env.port 3000) typeerror object #<object> has no method 'set' at object.<anonymous> and its pointing in C\program files\node\secondtest\bin\www:5:5
Truth is , I dont know how to deal with this error, because I dont get what it means. Is it because both my projects listen on port 3000?
I just started secondtest , I installed succesfully the dependencies with npm install and added this in app.js
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Thanks
EDIT
If I leave the default code in app.js and www I get no errors. If I replace the default code of app.js with mine, and I remove the
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
part from www, then I get no errors.
Because I guess app.set and app.get are depricated in express 4.2.0? Or because when I set an http server in my app.js code, conflicts the default www code? Either one of these, or I am really confused.
EDIT 2
This is the default code of the www
#!/usr/bin/env node
var debug = require('debug')('secondtest');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
Updated answer according to the updated question.
Since you're calling www and its code needs to set the port and listen to it, your secondtest code should not listen to the port. Instead it should export the Express app as follows:
// ...
module.exports = app;
The www will do the listening part.
Otherwise, the secondtest tries to start listening on a port while not exporting the Express app, and www tries to listen again on a variable app which is not an Express app, thus the error object #<object> has no method 'set'.
When you do var app = require('../app'); in another script, it is important so that this ../app script actually exports the Express app.
Old answer.
Do node app.js instead of using npm command.
Second, make sure the same port is not used by both processes at the same time. You can't listen to the same port unless you're in cluster mode.
Considering the following is the content of both firsttest and secondtest:
var http = require('http');
var express = require('express');
var app = express();
http.createServer(app).listen(process.env.port || 3000, function() {
console.log('Express app started');
});
app.get('/', function(req, res) {
res.send('Welcome!');
});
Do the following to start both apps:
Terminal 1: (the first app will default to port 3000).
$ node firsttest/app.js
Terminal 1:
$ export PORT=3001
$ node secondtest/app.js

Resources