JSON

Node.js + Express + MongoDB½Ì³Ì

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2015-11-11 10:30 ÎÒÒªÆÀÂÛ( )

Node.jsNode.js + Express + MongoDB½Ì³Ì °²×°£º STEP 1 INSTALL NODE.JS ÏÂÔØNode.js website£¬¼ÙÉè°²×°ÔÚc:\node STEP 2 INSTALL EXPRESSC:\nodenpm install -g express ÔÚNodeJsÃüÁîÐÐÊäÈëÉÏÊöÃüÁî STEP 3 ´´½¨Ò»¸öEXPRESS PROJECT ʹÓÃJade×÷Ϊģ°å£¬Êä

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

STEP 4 – ±à¼­ ÒÀÀµ

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": "*"
}

STEP 5 – °²×°ÒÀÀµ C:\node\nodetest1>npm install

ÏÖÔÚÄãÒѾ­ÓÐÁËÍêÕûµÄ×¼±¸£¬Çл»µ½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}

¡¡

1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

Ïà¹ØÎÄÕÂ
  • Meteor node.js ¸´ÖÆ(copy)ÎļþµÄ·½·¨

    Meteor node.js ¸´ÖÆ(copy)ÎļþµÄ·½·¨

    2016-01-25 19:50

  • express json

    express json

    2015-11-23 19:06

  • Express 3.0ÐÂÊÖÖ¸ÄÏÈëÃŽ̳Ì

    Express 3.0ÐÂÊÖÖ¸ÄÏÈëÃŽ̳Ì

    2015-11-23 13:11

  • Õë¶ÔJSONÊý¾ÝÀàÐÍÉè¼ÆµÄÊý¾Ý²Ö¿â--¼×¹ÇÎÄÅàѵ

    Õë¶ÔJSONÊý¾ÝÀàÐÍÉè¼ÆµÄÊý¾Ý²Ö¿â--¼×¹ÇÎÄÅàѵ

    2015-11-21 11:58

ÍøÓѵãÆÀ
ã