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.
Related
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);
});
});
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.
I'd like to set up some subdomains for my Node.js app. I've built my site with express.js, and now I'd just like to throw up a little web tool on a subdomain of my site. I've tried using the vhost middleware with little luck, but am open to other approaches.
Any help would rock!
Ideally, I could just drop a new express app in a sub directory change a few lines of code, maybe change some DNS settings at it would work. The reason I'd like this is so that I can reuse a fresh instance of stylus and jade with new layouts and css styles and so forth.
Here's my normal app.js, the commented line is the attempt to use vhost.
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.set('view options', { layout: false });
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use('/courses', function (req, res, next) {
var privates = require('./.private.json'),
couch = require('nano')('https://' + privates.dbCreds.username + ':' + privates.dbCreds.password + '#wamoyo.iriscouch.com/');
});
app.use(require('stylus').middleware({
src: __dirname + '/public'
}));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
// VHOST - SUBDOMAIN
// app.use(express.vhost('adventures.innovationbound.com', require('./adventures/index').app));
app.use(function (req, res, next) {
res.status(404);
res.render('four', {
title: "Innovation Bound",
now: new Date().getFullYear()
});
});
app.use(function (err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
});
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.get('/about', routes.about);
app.get('/services', routes.services);
app.get('/events', routes.events);
app.get('/blog', routes.blog);
app.post('/contact', routes.contact);
// Courses
// app.get('/heartbeat', routes.heartbeat);
app.get('/courses', routes.courses);
// Tools
app.get('/point', routes.point);
app.listen(3000, function() {
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
});
This is using express 2.5, I wouldn't mind migrating over to 3 if need be.
Backbone talking to node/express running locally os x 10.6.8. Trying to populate a clientside backbone model with fetch. Thinking I have fetch wrong. Most of app_stuff.js is cut-and-paste. Curious why node/express sees the request as OPTIONS instead of GET (or POST?) and if I have the terminology right. I think I can make it work like this but would rather pay the dues for some best practices while still an absolute newb.
backbone
(function($) {
var Stuff = Backbone.Model.extend({
});
var Collec = Backbone.Collection.extend({
model: Stuff,
url: 'http://localhost:3000/stuff'
});
var nstuff = new Collec;
nstuff.fetch(
{ data: $.param({ name: "helloworld"}) }
);
})(jQuery);
app_stuff.js
var express = require('express')
, routes = require('./routes')
, stuff = require('./routes/stuff')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
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')));
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
app.get('/stuff', stuff.index);
app.options('/stuff', function() { console.log("stuffed"); });
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
terminal output
$ node timeline/app_stuff.js
Express server listening on port 3000
stuffed
I get this error:
500 Error: ENOENT, open 'C:\Users\Gilbert\WebstormProjects\games\views\layout.hbs
but my project has no reference to this file. when trying to render a simple test page with HBS, I used a pregenerated express app that created a layout.jshtml file but deleated this file.
app.js:
/**
* 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', 'hbs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')({ src: __dirname + '/public' }));
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'));
});
routes/index.js:
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('hi.hbs', { title: 'Express' });
};
views/hi.hbs:
<h1>IT WORKS</h1>
With HBS you must manually disable need for a layout with layout:false this should be done in the view options to make it app global.