Instalamos node.js + npm (ya lo vimos aquí)
Instalamos express.js
$ npm install -g express /home/b3ni/local/bin/express -> /home/b3ni/local/lib/node_modules/express/bin/express qs@0.4.0 /home/b3ni/local/lib/node_modules/express/node_modules/qs mkdirp@0.0.7 /home/b3ni/local/lib/node_modules/express/node_modules/mkdirp mime@1.2.4 /home/b3ni/local/lib/node_modules/express/node_modules/mime formidable@1.0.8 /home/b3ni/local/lib/node_modules/express/node_modules/connect/node_modules/formidable connect@1.8.1 /home/b3ni/local/lib/node_modules/express/node_modules/connect express@2.5.1 /home/b3ni/local/lib/node_modules/express
Creamos carpeta para el proyecto, en mi caso, mi proyecto git + bitbucket (ya lo vimos aquí)
$ cd ~/git/proyecto $ express destination is not empty, continue? yes create : . create : ./package.json create : ./app.js create : ./public create : ./routes create : ./routes/index.js create : ./views create : ./views/layout.jade create : ./views/index.jade create : ./public/javascripts create : ./public/images create : ./public/stylesheets create : ./public/stylesheets/style.css dont forget to install dependencies: $ cd . && npm install
Instalamos dependencia si son necesarias.
$ npm install -g express /home/b3ni/local/bin/express -> /home/b3ni/local/lib/node_modules/express/bin/express qs@0.4.0 /home/b3ni/local/lib/node_modules/express/node_modules/qs mkdirp@0.0.7 /home/b3ni/local/lib/node_modules/express/node_modules/mkdirp mime@1.2.4 /home/b3ni/local/lib/node_modules/express/node_modules/mime formidable@1.0.8 /home/b3ni/local/lib/node_modules/express/node_modules/connect/node_modules/formidable connect@1.8.1 /home/b3ni/local/lib/node_modules/express/node_modules/connect express@2.5.1 /home/b3ni/local/lib/node_modules/express
Ya podemos arrancar el server con
$ node app.js Express server listening on port 3000 in development mode
Vamos a escribir el mensaje Hello World. Para ello miramos la vista que se está renderizando.
// Routes app.get('/', routes.index); En la configuración tenemos: 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')); });
Podemos ver que los ficheros éstaticos se sirven con node.js y están ela carperta /public. Las vistas estás en /views. Editamos el template /views/index.jade y lo cambiamos a nuestro gusto
h1= title p Hello World
http://expressjs.com/guide.html