"Node. js actual" blog Instance express4.x

Source: Internet
Author: User

Roughly read a Nodejs book for a while, have a preliminary understanding of grammar. But I still can't write an example. Recently my seniors gave me this book is very introductory "node. JS Real" electronics industry press. After all, the library borrowed, the publication time is two years ago. A lot of code has been updated. In particular, the Express 4.x has a lot of modifications compared to the Express 3.x. such as the middleware independent out. The advantage is that there is. So the update of Express will not be affected by these middleware the disadvantage of perhaps mainly for me this kind of novice. Most of the textbooks are express 3.x or even 2.x. It is difficult to learn this way. Maybe no one will see this. Even if you can't solve other people's problems and want to record, maybe one day I will forget.


Middleware issues refer to Http://www.tuicool.com/articles/YBVRvuJ


I use the Express version:

C:\users\meow>express-v

4.13.1


An error that may be found in Express 4 is that express is not an internal or external command

Because Express 4 streamlines many files, we need to install additional command Tools

$ npm install-g express-generator

Under your own preset path, enter:

$ EXPRESS-E Blog


$CD Blog & npm Install

Create a new project with Express and specify the use of the Ejs template engine

express4.x is not enabled using $node app: $NPM start

You can then access the Http://localhost:3000/


    1. Modify Routes\index.js//differs from the code in the book

var express = require (' Express '); var router = Express. Router (); Router.get ('/', function (req, res) {res.render (' index ', {title: ' Home '})}); Router.get ('/reg ', function (req, RES) {Res.render (' reg ', {title: ' Registered ')}), Router.post ('/reg ', function (req, res) {}), Router.get ('/login ', function (req , res) {res.render (' login ', {title: ' Login ')}), Router.post ('/login ', function (req, res) {}), Router.get ('/post ', function (req, res) {Res.render (' post ', {title: ' published ')}); Router.post ('/post ', function (req, res) {}), Router.get ('/logout ', function (req, res) {}); module.exports = router;


2. Installing MongoDB



Tutorial: http://www.runoob.com/mongodb/mongodb-window-install.html


3. Modify Package.js

"Dependencies":

Add Inside:

"MongoDB": "*", "Connect-mongo": "*", "express-session": "*"


Notice the top, split

Execute $NPM Install


4. root directory creation settings.js//consistent with the code in the book. The MongoDB database I created by the above URL tutorial is called db to prevent errors from being slightly different from the code in the book.

Module.exports = {cookiesecret: ' MyBlog ', DB: ' DB ', Host: ' localhost '};


5. Create the folder models in the directory and create Db.js in it to add the following code:

var settings = require ('.. /settings '), Db = require (' MongoDB '). Db, Connection = require (' MongoDB '). Connection, Server = require (' MongoDB '). Server;module.exports = new Db (settings.db, New Server (Settings.host, connection.default_port,{}), {safe:true});


6. Open the app.js in the appropriate location to add://With the book there are many different carefully examined

var Mongostore = require (' Connect-mongo ') (session), var settings = require ('./settings ');


App.use (Session ({Secret:settings.cookieSecret, key:settings.db, cookie:{maxage:1000*60*60*24*30}, Store:new Mongo Store ({//db:settings.db URL: ' mongodb://localhost/db '}));


7. Modify Views\index.ejs

<%-include header%> This is the home page <%-include footer%>


New Header.ejs under 8.views

<! DOCTYPE html>


9. New Footer.ejs

</article></body>


10. Modify Public\stylesheets\style.css

*{padding:0;margin:0;} Body {  width: 600px;  margin: 2em auto;  padding: 0  2em;  font-size: 14px;  font-family:  "Microsoft yahei";} p{line-height: 24px;margin: 1em 0;} header{padding: .5em 0;border-bottom: 1px solid  #cccccc;} nav{position: fixed;left: 12em;font-family:  "Microsoft yahei";font-size: 1.1em; Text-transform: uppercase;width: 9em;text-align: right;} nav a{display: block;text-decoration: none;padding: .7em 1em;color:  #000000;} nav a:hover{background-color:  #ff0000;color:  #f9f9f9;-webkit-transition:color .2s  Linear;} Article{font-size: 16px;padding-top: .5em;} article a {color:  #dd0000; text-decoration: none;} article a:hover{color:  #333333; text-decoration: underline;}. info{font-size: 14px;}


You can then execute $NPM start and access the view!

Initially written in:

Https://github.com/justmeow/blog_beta.git

"Node. js actual" blog Instance express4.x

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.