Node.js Node.js + Express + MongoDB½Ì³Ì
¡¡¡¡°²×°£º
STEP 1 – INSTALL NODE.JSÏÂÔØ Node.js website £¬¼ÙÉè°²×°ÔÚc:\node
STEP 2 – INSTALL EXPRESS C:\node>npm install -g expressÔÚNodeJsÃüÁîÐÐÊäÈëÉÏÊöÃüÁî
STEP 3 – ´´½¨Ò»¸öEXPRESS PROJECTʹÓÃJade×÷Ϊģ°å£¬ÊäÈ룺
C:\node>express --sessions nodetest1Êä³ö£º
C:\node>express --sessions nodetest1
create : nodetest1
create : nodetest1/package.json
create : nodetest1/app.js
create : nodetest1/routes
create : nodetest1/routes/index.js
create : nodetest1/routes/user.js
create : nodetest1/views
create : nodetest1/views/layout.jade
create : nodetest1/views/index.jade
create : nodetest1/public/images
create : nodetest1/public/javascripts
create : nodetest1/public
create : nodetest1/public/stylesheets
create : nodetest1/public/stylesheets/style.css
install dependencies:
$ cd nodetest1 && npm install
run the app:
$ node app
expressÒѾÔÚnodetest1 ´´½¨Ò»¸öÎļþpackage.json£¬´ò¿ª±à¼£º
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.4",
"jade": "*"
}
}
¸Ä±äÒÀÀµ£¬¼ÓÈëMongoDB ºÍ Monk.µ÷Óãº
"dependencies": {
"express": "3.4.4",
"jade": "*",
"mongodb": "*",
"monk": "*"
}
ÏÖÔÚÄãÒѾÓÐÁËÍêÕûµÄ×¼±¸£¬Çл»µ½nodetest1 ÊäÈ룺
C:\node\nodetest1>node app.jsÊä³ö£º
Express server listening on port 3000ä¯ÀÀÆ÷´ò¿ª:3000
½çÃæÈçÏ£º
ʵÏÖHello World
ʹÓà Sublime TextÀ´±à¼´úÂë¡£
ÔÚnodetest1 ´ò¿ª app.js. ¼ÓÈëÈçÏ£º
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
Õâ¾Í²úÉúÁËÒ»¶Ñ»ù±¾µÄJavaScript±äÁ¿£¬²¢½«Æä°óµ½Ä³Ð©°ü£¬ÒÀÀµ¹Øϵ£¬½Úµã¹¦ÄܺÍ·ÓÉ¡£Â·ÓÉÊÇÓеãÏñµÄÄ£ÐͺͿØÖÆÆ÷ÔÚ´ËÉèÖõÄ×éºÏ - ËûÃÇÖ¸»Ó½»Í¨£¬Ò²??°üº¬Ò»Ð©±à³ÌÂß¼¡£
¿ªÊ¼±à³Ì£¬ÊäÈ룺
var app = express(); ÕâÆäÖкÜÖØÒªµÄ¡£ËüʵÀý»¯Express£¬²¢·ÖÅäÎÒÃǵÄÓ¦ÓóÌÐò±äÁ¿¸øËü¡£ÏÂÒ»½Ú½«Ê¹ÓÃÕâ¸ö±äÁ¿À´ÅäÖÃÒ»¶ÑµÄ¶«Î÷¡£// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__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')));
ÕâÊǸæËßexpressÄÄÀï·¢ÏÖÊÓͼview£¬Ê²Ã´ÒýÇæÓÃÀ´äÖȾÕâЩÊÓͼ(jade)£¬È»ºóµ÷ÓÃÕâЩ·½·¨¡£
×îºóÒ»ÐÐÊǸæËßexpress·þÎñ¾²Ì¬¶ÔÏó£º/public/ dir£¬µ«Ê¹ËûÃÇʵ¼ÊÉÏ¿´ÆðÀ´ËûÃÇÊÇ´Ó¶¥²ãµ½À´¡£ÀýÈ磬ͼÏñĿ¼Ϊc:\node\nodetest1\public\image£¬·ÃÎÊÊÇ:3000/images
ÄãÐèÒª¸Ä±ä:
app.use(express.bodyParser());µ½£º
app.use(express.urlencoded());ÕâÊÇΪÁ˱ÜÃâÄãµÄ½Úµã¿ØÖÆ̨³öÏÖһЩ¾¯¸æ/¡£
ÏÂÃæÊÇ×öһЩ´íÎó¼ì²é£º
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
ÏÂÃæÊǸæËß·ÓÉʹÓÃÄĸöURI:
app.get('http://www.jdon.com/', routes.index);
app.get('/users', user.list);
´úÂë¿Æ¼¼user±äÁ¿Ó³Éäµ½/routes/user.js?
ÏÂÃæÊÇ´´½¨Æô¶¯·þÎñÆ÷£º
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
ÏÂÃæÎÒÃÇ¿ªÊ¼ÏÔʾHello, World!£¬ÔÚapp.jsβ¶Ë¼ÓÈ룺
app.get('/helloworld', routes.helloworld);Èç¹ûÄãÕâʱÖØÆô·þÎñÆ÷£¬ä¯ÀÀ·ÃÎÊ:3000/helloworld »á³ö´í£¬ÒòΪÎÒÃÇûÓÐÅäÖ÷ÓɶÔÓ¦µÄÒ³Ãæ¡£
ÎÒÃÇ´ò¿ªC:\NODE\NODETEST1\ROUTES\INDEX.JS£¬ÈçÏ£º
/*
* GET home page.
*/
exports.index = function(req, res){
res.render('index', { title: 'Express' });
};
ÎÒÃÇÒªÔÚ/viewsÏ´´½¨helloworld ,ÔÚ¸ÃÎļþβ¶Ë¼ÓÈ룺
exports.helloworld = function(req, res){
res.render('helloworld', { title: 'Hello, World!' });
};
µ±È»ÎÒÃDz»±ØÒª±à¼¾ßÌåhelloworldÒ³Ã棬ÒòΪjadeÄ£°åΪÎÒÃÇʵÏÖ£¬´ò¿ªviews Ŀ¼£¬´ò¿ªindex.jade. Îļþ£¬Áí´æ³É£ºhelloworld.jade
±à¼C:\NODE\NODETEST1\VIEWS\HELLOWORLD.JADE
extends layout
block content
¡¡¡¡h1= title
¡¡¡¡p Welcome to #{title}
¡¡