I installed nodejs using homebrew just now.
I then created a folder, and installed express:
npm install -g express
I then created a new project using:
express -H --css less --sessions foo
Now when I try and run the app:
node app.js
I get this:
/Users/blah/dev/testing/nodetest1/foo/node_modules/less-middleware/lib/middleware.js:50
throw new Error('Please update your less-middleware usage: http://goo.gl/Y
^
Error: Please update your less-middleware usage: http://goo.gl/YnK8p0
at module.exports.less.middleware (/Users/blah/dev/testing/nodetest1/foo/node_modules/less-middleware/lib/middleware.js:50:11)
at Object.<anonymous> (/Users/snad/dev/testing/nodetest1/foo/app.js:26:35)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
My app.js looks like:
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('less-middleware')({ src: path.join(__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);
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
The middleware in node. js is a function that will have all the access for requesting an object, responding to an object, and moving to the next middleware function in the application request-response cycle.
Node. js programs can be slow due to a CPU/IO-bound operation, such as a database query or slow API call. For most Node. js applications, data fetching is done via an API request and a response is returned.
The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.
app. get is called when the HTTP method is set to GET , whereas app. use is called regardless of the HTTP method, and therefore defines a layer which is on top of all the other RESTful types which the express packages gives you access to.
I changed the line that was:
app.use(require('less-middleware')({ src: path.join(__dirname, 'public') }));
to:
app.use(require('less-middleware')(path.join(__dirname, 'public')));
and it works now. Strange how this was a brand new app with a fresh install of nodejs and express.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With