顯示具有 Node.js Blog 標籤的文章。 顯示所有文章
顯示具有 Node.js Blog 標籤的文章。 顯示所有文章

2017年3月15日 星期三

2017年3月14日 星期二

[Node.js] Create a Blog - Start add more function (5/x)

First step:
Edit layout of home page.

--- views/index.jade.bak        2017-03-15 03:08:39.126666666 +0000
+++ views/index.jade    2017-03-15 03:08:48.830000000 +0000
@@ -1,5 +1,21 @@
-extends layout
+doctype html
+html
+       head
+               title= 'Blog System'
+       body
+               div(style='float:left;margin:10px;')
+                       a(href='/') Home

-block content
-  h1= title
-  p Welcome to #{title}
+               div(style='float:right')
+                       if (username && authenticated)
+                               a(href='/users/signout', style='margin:10px;') Sign Out
+                               a(href='/users/add_article', style='margin:10px;') Add Article
+                               a(href='/users/profile', style='margin:10px;') Profile
+                               span #{username}
+                       else
+                               a(href='/users/signin', style='margin:10px;') Log In
+                               a(href='/users/register', style='margin:10px;') Register
+                               a(href='/users/forget', style='margin:10px;') Forgot password
+
+       div(style='padding:50px;')
+               h1= title

Edit routing rule.

--- routes/index.js.bak 2017-03-15 03:14:35.019999998 +0000
+++ routes/index.js     2017-03-15 03:14:47.996666664 +0000
@@ -3,7 +3,9 @@

 /* GET home page. */
 router.get('/', function(req, res, next) {
-  res.render('index', { title: 'Express' });
+    res.locals.username = req.session.name ;
+    res.locals.authenticated = req.session.logined;
+    res.render( 'index', {title : 'Blog System'});
 });

 module.exports = router;

Second step:
Design the layout of webpage.

doctype html
html
    head
        title= Register
    body
        div(style='float:left;margin:10px;')
            a(href='/') Home

        div(style='float:right')
                a(href='/users/signin', style='margin:10px;') Log In
                a(href='/users/forget', style='margin:10px;') Forgot password

        div(style='padding:50px;')
            form(name="login", action="/apis/login", method="post")
                h1 Register
                div
                    p
                        label(style='padding-right:50px') Username or e-mail
                        input(type="text", name="user", placeholder='username@example.com')
                    p
                        label(style='padding-right:110px')  password
                        input(type="password", name="passwd")
                p
                    span(style='padding-right:260px')
                    input(type="submit", name="submit", value="Sing Up")
--- routes/users.js.bak 2017-03-15 03:39:48.486666666 +0000
+++ routes/users.js     2017-03-15 03:42:55.686666666 +0000
@@ -3,22 +3,36 @@

 /* ______________. */
 router.get('/register', function(req, res, next) {
-  res.send("This is the register page.");
+    if (req.session.logined) {
+        res.redirect('/');
+        return;
+    }
+    res.render('users/register');
 });

 /* ______________. */
 router.get('/signin', function(req, res, next) {
-  res.send("This is the signin page.");
+    if (req.session.logined) {
+        res.redirect('/');
+        return;
+    }
+    res.render('users/signin');
 });

 /* ______________. */
 router.get('/signout', function(req, res, next) {
-  res.send("This is the signout page.");
+    req.session.logined = false;
+    res.redirect('/');
+    res.end();
 });

 /* ____________. */
 router.get('/forget', function(req, res, next) {
-  res.send("This is the forget page.");
+    if (req.session.logined) {
+        res.redirect('/');
+        return;
+    }
+    res.render('users/forget');
 });
--- routes/apis.js.bak  2017-03-15 03:41:16.383333333 +0000
+++ routes/apis.js      2017-03-15 03:41:49.346666665 +0000
@@ -8,7 +8,14 @@

 /* __________________. */
 router.post('/login', function(req, res, next) {
-  res.send("This is the login function.");
+    if ((!req.body.user) || (!req.body.passwd)) {
+        res.redirect('register');
+        return;
+    }
+    req.session.name = req.body.user;
+    req.session.passwd = req.body.passwd;
+    req.session.logined = true;
+    res.redirect('/');
 });


Reference:

[Node.js] Create a Blog - Set routing into blog function (4/x)

Add routing path into app.js
--- app.js.bak  2017-03-15 02:31:40.749999989 +0000
+++ app.js      2017-03-15 02:32:42.653333318 +0000
@@ -8,6 +8,7 @@

 var index = require('./routes/index');
 var users = require('./routes/users');
+var apis = require('./routes/apis');

 var app = express();

@@ -31,6 +32,7 @@

 app.use('/', index);
 app.use('/users', users);
+app.use('/apis', apis);

 // catch 404 and forward to error handler
 app.use(function(req, res, next) {

Edit users.js under routes
When user get info from “users”, “users” will send back info to webpage.
So use “router.get”.

--- users.js.bak    2017-03-15 02:31:34.113333328 +0000
+++ users.js    2017-03-15 02:32:00.576666660 +0000
@@ -1,9 +1,44 @@
 var express = require('express');
 var router = express.Router();

-/* GET users listing. */
-router.get('/', function(req, res, next) {
-  res.send('respond with a resource');
+/* 使用者註冊頁面. */
+router.get('/register', function(req, res, next) {
+  res.send("This is the register page.");
+});
+
+/* 使用者登入頁面. */
+router.get('/signin', function(req, res, next) {
+  res.send("This is the signin page.");
+});
+
+/* 使用者登出頁面. */
+router.get('/signout', function(req, res, next) {
+  res.send("This is the signout page.");
+});
+
+/* 忘記密碼頁面. */
+router.get('/forget', function(req, res, next) {
+  res.send("This is the forget page.");
+});
+
+/* 使用者管理頁面. */
+router.get('/profile', function(req, res, next) {
+  res.send("This is the profile page.");
+});
+
+/* 新增文章頁面. */
+router.get('/add_article', function(req, res, next) {
+  res.send("This is the add_article page.");
+});
+
+/* 修改文章頁面. */
+router.get('/modify/:id', function(req, res, next) {
+  res.send("This is the modify page.");
+});
+
+/* 訪客留言頁面. */
+router.get('/message/:id', function(req, res, next) {
+  res.send("This is the message page.");
 });

 module.exports = router;
User will input some info, so use “router.post”.
var express = require('express');
var router = express.Router();

/* 使用者刪除文章功能. */
router.get('/delete/:id', function(req, res, next) {
  res.send("This is the delete function.");
});

/* 使用者登入會員功能. */
router.post('/login', function(req, res, next) {
  res.send("This is the login function.");
});

/* 使用者新增文章功能. */
router.post('/add', function(req, res, next) {
  res.send("This is the add function.");
});

/* 使用者更新文章功能. */
router.post('/update/:id', function(req, res, next) {
  res.send("This is the update function.");
});

/* 文章留言功能. */
router.post('/comment/:id', function(req, res, next) {
  res.send("This is the comment function.");
});

module.exports = router;

[Node.js] Create a Blog - Add cookie-based session (3/x)

cookie-based session:
Write all information on the cookie. Server don't need to store information.When client want to ask information from server. Client take cookie to server, server understand all information from cookie.And server send back data to client.
--- blog-system/app.js     2017-03-15 02:04:02.049999999 +0000
+++ blog-system/app.js     2017-03-15 02:00:47.430000007 +0000
@@ -4,6 +4,7 @@
 var logger = require('morgan');
 var cookieParser = require('cookie-parser');
 var bodyParser = require('body-parser');
+var cookieSession = require('cookie-session')

 var index = require('./routes/index');
 var users = require('./routes/users');
@@ -22,6 +23,12 @@
 app.use(cookieParser());
 app.use(express.static(path.join(__dirname, 'public')));

+// ____cookieSession
+app.use(cookieSession({
+    key: 'node',
+    secret: 'HelloExpressSESSION'
+}))
+
 app.use('/', index);
 app.use('/users', users);
If modify "package.json", please type "npm install" to install dependencies.

--- test/blog-system/package.json       2017-03-15 02:04:02.049999999 +0000
+++ blog-system/package.json    2017-03-15 02:01:56.426666659 +0000
@@ -12,6 +12,8 @@
     "express": "~4.14.1",
     "jade": "~1.11.0",
     "morgan": "~1.7.0",
-    "serve-favicon": "~2.3.2"
+    "serve-favicon": "~2.3.2",
+    "cookie-session": "*",
+    "mongoose": "*"
   }
 }

[Node.js] Create a Blog - Create blog template and start web server (2/x)

Install express plugin
npm -g install express-generator

Create a blog template
express blog-system

cd blog-system
According to package.json will need to install dependencies.
Please type following command.
npm install

Start web server
DEBUG=blog-system:* npm start

Open web browser to browse the webpage.
http://192.168.56.101:3000/

2017年3月12日 星期日