dev

Base application with mongodb + expressjs

1. Tạo project tên blog
express blog
blog
│   README.md
|   app.js
|   package.json
│
└───bin
    │   www
    │
└───models
    │   Blog.js
    │   db.js
    |
└───node_modules
└───public
    ├───images
    ├───javascripts
    ├───stylesheets
    |
└───routes
    |   blog.js
    │   index.js
└───views
    │   error.jade
    |   index.jade
    |   layout.jade
    └───blog
        │   edit.jade
        │   index.jade
        |   new.jade
2. set up mongoDB brew update
brew install mongodb
MongoDB shell 
mongo
use blogdb 
3. Setup Monogoose for mongoDB 
models/ db.js: kết nối vs mongoDB 
var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/blogdb');
add db.js on app.js 
db = require('./models/db'),
4. Install Dependencies
npm install npm install mongoose --save 
npm install body-parser --save
npm install method-override --save 
5. Create the Model and Schema 
var mongoose = require('mongoose'); 
var blogSchema = new mongoose.Schema({ 
 title: String, 
 content: Number, 
 pushed_date: { type: Date, default: Date.now }
}); 
mongoose.model('Blog', blogSchema); 
add Blog.js on app.js 
blog = require('./models/Blog'), 
6. Create the Controller 
/routes
blog.js
7. Add The Route on app.js
var blog = require('./routes/blog');
app.use('/blog', blog); 
8. Add The Views
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s