app is not initialized in node express - node.js

I have just installed node and express. When I run the comman node app.js it gives me following error.
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
/var/www/nodeexpress/nodetest1/app.js:38
console.log("Express server listening on port %d in %s mode", app.address().
^
TypeError: undefined is not a function
at Server.<anonymous> (/var/www/nodeexpress/nodetest1/app.js:38:69)
at Server.g (events.js:196:16)
at Server.EventEmitter.emit (events.js:101:17)
at net.js:1159:10
at process._tickCallback (node.js:339:11)
at Function.Module.runMain (module.js:492:11)
at startup (node.js:124:16)
at node.js:803:3
And My app.js looks like
var express = require('express')
, routes = require('./routes');
//var app = module.exports = express.createServer();
var app = express();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.listen(8080, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
It seems that express not getting initalized. My node version is v0.11.13-pre and I hav0.11.13-pree installed express version using package.json as 3.4.4

use this.address().port than app.address().port
address() is a function property of the http object to which the listen function is applied
From connect proto.js:
app.listen = function(){
var server = http.createServer(this);
return server.listen.apply(server, arguments);
};
as for the connect.multipart() warning, check this answer: How to get rid of Connect 3.0 deprecation alert?

Here is the solution
You need to remove this line because the bodyParser method is not in this version.
app.use(express.bodyParser());
replace with
app.use(express.json());
app.use(express.urlencoded());
Let me know if still facing any issue.

Related

socket.io/socket.io.js - not found. Node.js, express.js and socket.io app

Im getting error that socket.io/socket.io.js cannot be loaded. I've made this:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes');
var io = require('socket.io').listen(express);
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
io.on('connection', function(socket){
console.log('a user connected');
});
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
My layout.jade file looks like this:
doctype html
html
head
title= title
script(src='//code.jquery.com/jquery-1.11.2.min.js')
script(src='/bootstrap/js/bootstrap.min.js')
link(rel='stylesheet', href='/stylesheets/style.css')
link(rel='stylesheet', href='/bootstrap/css/bootstrap.min.css')
script(src='/socket.io/socket.io.js')
script.
var socket = io();
body!= body
and package.json file:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "2.5.8",
"jade": ">= 0.0.1",
"socket.io": "^1.3.2"
}
}
I installed socket.io with npm, all files are in node_modules catalog. I've tried to copy socket.io.js file into /public but there's an error too. I tried to solve that problem in many ways - with socket.io docs, chat tutorial and StackOverflow post. Every time when i try to you socket.io problem occurs. Is anything wrong in my code? What should i do to make this thing work correctly?
Does running npm update in terminal fix this? (You might need to run it with elevated privileges with sudo or equivalent)
When you run an application with Node.js, it doesn't automatically install the dependencies. You'll need to use NPM (Node package manager). Their is an update command. In the directory with your package.json file, run this:
npm update
You might need elevated privileges. This is what it looks like on Mac OS X/Linux:
sudo npm update
You are very much there, let me change few things in your app.js.
Try it now.
var express = require('express')
, routes = require('./routes');
var app = module.exports = express.createServer();
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get('/', routes.index);
var server = app.listen(3000, function(){
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
var io = require("socket.io").listen(server);
io.on("connection",function(socket){
console.log("Application connected");
socket.on('disconnect',function(){
console.log("Application Disconnected");
});
socket.on('ABC Event',function(value){
console.log("ABC event catched from client value passed is :"+value);
//do something
io.emit('new event',passSomeJSONtoClient);
});
});

Accessing the Backbone model.save() POST payload in the node-express backend

I have written a code snippet using backbone which POST's data to the urlRoute .
(function(){
"use strict"
window.Course = Backbone.Model.extend({
defaults:{
title:''
},
urlRoot:"courses/"
});
var courses = new Course({title:"Sending a Post request to the node-express backend,but how to access this in the backend"});
courses.save();
})();
I have used node.js - express framework in the backend ,i want to know how to retrieve the value of the title attribute using the app.post('/courses',function(req,res){}) method .
This is the node.js backend ,The control comes to the app.post method , but just want ot how to the access the model value in the posted data .
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.post('/courses',function(req,res) {
console.log('Request successfully recieved');
console.log("how do i get the posted data here !!");
});
app.get('/', routes.index);
app.get('/users', user.list);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
You can find your request data in req.body (more on that http://expressjs.com/api.html#req.body).
In your case you can do like this:
app.post('/courses',function(req,res) {
console.log(req.body.title);
});

Express Node.js not responding on live server

i have written a simple hello world program in express node.js and when i call it through Curl it's response is correct i.e it displays Hello world on the console but when i call the url from browser i get could not connect error.Here is my code:
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3039);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/partner', function(req, res){
res.send('Hello World');
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
You have either a DNS issue or a Firewall issue.
Perhaps this answer will help you.
Which cloud service you are using for node.js .share url of this page

JSHTML not working on Express.js in Node.js

I don't seem to get JSHTML to work as a template engine on Express.js in Node.js. When I install my Express.js application and a basic application is created for me, and I run it I get this error message:
500 TypeError: Property 'engine' of object #<View> is not a function
at View.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/view.js:75:8)
at Function.app.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/application.js:504:10)
at ServerResponse.res.render (/Users/blackbook/nodejs/ds/node_modules/express/lib/response.js:677:7)
at exports.index (/Users/blackbook/nodejs/ds/routes/index.js:7:7)
at callbacks (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:165:11)
at param (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:139:11)
at pass (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:146:5)
at Router._dispatch (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:173:5)
at Object.router (/Users/blackbook/nodejs/ds/node_modules/express/lib/router/index.js:33:10)
at next (/Users/blackbook/nodejs/ds/node_modules/express/node_modules/connect/lib/proto.js:190:15)
My app.js looks like this (it's what Express.js created for me):
/**
* Module dependencies.
*/
var express = require('express')
, routes = require('./routes')
, http = require('http')
, path = require('path');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jshtml');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
I have this installation:
Node.js v.0.8.5
Express.js#3.0.0rc2
jshtml#0.2.3
JSHTML currently works with Express.js 2. There are plans on getting the engine to work with Express.js 3, but currently I am too busy with enjoying the summer! Expect a fix for this problem in the winter!
According to https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x you can use app.engine for 2-x compatibility.
e.g.
var fs = require("fs");
var jshtml = require("jshtml");
app.engine("jshtml", function (path, options, fn) {
fs.readFile(path, 'utf8', function (err, str) {
if (err) return fn(err);
str = jshtml.compile(str,options)(options).toString();
fn(null, str);
});
});
consolidate.js is used as a bridge between many template engines and express. If your engine isn't supported checkout the source code. Most engines need like 15 lines of code to implement.
I have it working in my project and will probably issue a pull request soon but for now look at my comment in
https://github.com/elmerbulthuis/jshtml/issues/5
Try the following. It works for me, as like you.
Firstly, install jshtml-express via npm and then do the following.
var app = express();
**app.engine('jshtml', require('jshtml-express'));**
// All environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jshtml');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
I hope it will work for you as well.

Node.js takes 4.0 min to serve up style sheet

I've been playing around with Node.js/Express and following the tutorial here and when I try to launch the app.js it takes 4 minutes for it to timeout while sending the style.css. I'm using Chrome to monitor the network traffic. Am I missing something in setting up the stylus?
This is my app.js:
var express = require('express');
var ArticleProvider = require('./articleprovider-memory').ArticleProvider;
var app = module.exports = express.createServer();
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(require('stylus').middleware({debug: true, src: "#{__dirname}/../public", dest: "#{__dirname}/../public"}));
app.use(express.static(__dirname+'/public'));
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
var articleProvider= new ArticleProvider();
app.get('/', function(req, res){
articleProvider.findAll( function(error,docs){
res.render('index.jade', { locals: {
title: 'Blog',
articles:docs
}
});
});
});
app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
EDIT:
I found the problem.
var app = module.exports = express.createServer();
I removed the module.exports and it seems to be working.
EDIT:
If you have this problem with Nodejs simply re-install it.

Resources