/node-js/

Node.js, Express, Jade and simple CRUD

2014-08-11 18:58:39

I use in this example:

Before start you have to install

nodejs and npm

sudo apt-get update
sudo apt-get install nodejs
sudo apt-get install npm

node_modules:

npm install express
npm install jade
npm install body-parser

Project structure

server.js

var express = require('express');
var bodyParser = require('body-parser')
var path = require('path');
var methodOverride = require('method-override')

var post = require('./module/post');

var app = express();

app.set('views', __dirname + '/views');
app.set('view engine', 'jade');

app.use(express.static(path.join(__dirname, 'public')));
app.use(bodyParser.urlencoded({ extended: false }))

app.get('/', post.index);
app.get('/post/create', post.form);
app.post('/post/create', post.save);
app.get('/post/details/:id', post.details);
app.get('/post/edit/:id', post.edit);
app.post('/post/update', post.update);
app.get('/post/delete/:id', post.remove);

app.listen(3000);

post.js

var util = require('util');
var db = require('./postMemoryDb');

exports.index = function (req, res) {
    res.render('post/index', { posts: db.list()});
};

exports.form = function (req, res) {
    res.render('post/form');
};

exports.details = function (req, res) {
    var post = db.getById(req.params.id);
    res.render('post/details', {post: post});
};

exports.edit = function (req, res) {
    var post = db.getById(req.params.id);
    res.render('post/edit', {post: post})
}

exports.save = function (req, res) {
    db.add({name: req.body.name, desc: req.body.desc});
    res.redirect('/');
};

exports.update = function (req, res) {
    db.update({id: req.body.id, name: req.body.name, desc: req.body.desc});
    res.redirect('/');
};

exports.remove = function (req, res) {
    db.remove(req.params.id);
    res.redirect('/');
};

postMemoryDb.js

var memoryDb = {};
var id = 0;

var list = function () {
    return memoryDb;
};

var add = function (item) {
    id = id + 1;
    item.id = id;
    memoryDb[item.id] = item;
};

var getById = function (id) {
    return memoryDb[id];
};

var update = function (item) {
    console.log(item.id);
    memoryDb[item.id] = item;
};

var remove = function (id) {
    delete memoryDb[id];
};

exports.list = list;
exports.add = add;
exports.getById = getById;
exports.memoryDb = memoryDb;
exports.remove = remove;
exports.update = update;

index.jade

doctype
html
    head
        title
    body
        h1 Posts
        ul
        each item in posts
            li Item #{item.name} - #{item.desc} -
                a(href='/post/details/#{item.id}') Details -
                a(href='/post/edit/#{item.id}') Edit -
                a(href='/post/delete/#{item.id}') Delete
        div
            p
                a(href='post/create') Add new

form.jade

doctype
html
    head
        title
    body
        h1 New post
        form(id='form', action='/post/create', method='POST')
            input(id='name', name='name', placeholder='Name')
            input(id='desc', name='desc', placeholder='Desc')
            input(type='submit', value='Add post')

edit.jade

doctype
html
    head
        title
    body
        h1 New post
        form(id='form', action='/post/update', method='POST')
            input(id='id', name='id', type='hidden', value='#{post.id}')
            input(id='name', name='name', value='#{post.name}')
            input(id='desc', name='desc', value='#{post.desc}')
            input(type='submit', value='Update post')

details.jade

doctype
html
    head
        title
    body
        p Name: #{post.name}
        p Desc: #{post.desc}