Get started with nodejs web development: Simple-TODO Nodejs implementation version

Source: Internet
Author: User
Tags http redirect node server
Document directory
  • How to access the database
  • URL Routing and Controllers
  • Database initialization and startup of Web Processes
  • Love
Cause

I can see various python version implementations of simple todo...
Now that there are so many python versions, I have compared and implemented a Simple-TODO nodejs version: Node TODO, And the template and style are all copied from the web. py version.

Source Code & Demo
  • Source code: https://github.com/fengmk2/todo
  • Online demo: http://api.yongwo.de: 3888

Code directory

The directory is clear. public stores static files, views stores templates, and controllers processes business logic,
You can also configure config. js, which is the main web entry server. js.

Used third-party nodejs Module
  • Express: Web framework. Currently, nodejs is the most widely used web framework.
  • Ejs: Template rendering engine used to generate dynamic content
  • Node-mysql: mysql driver implemented by pure javascript, and no mysql package dependency can be installed.

The three modules can be directly obtained through npm installation:

$ npm install express ejs mysql
Config. js

Configurable information:
* Website name
* Listening port number. The default value is 8080.
* Database configuration information

How to access the database

In the config. js file, the link object of the database is maintained in the following way:

var db = exports.db = new require('mysql').Client(db_options);db.connect(function(err) {    if(err) {        console.error('connect db ' + db.host + ' error: ' + err);        process.exit();    }});

In this way, we can directly use the db object elsewhere.

Server. js

Express can help us implement static file processing, cookie processing, request parameter processing and other things by default. You only need to configure these functions to use them.

var app = express.createServer();app.use(express.static(__dirname + '/public', {maxAge: 3600000 * 24 * 30}));app.use(express.cookieParser());app.use(express.bodyParser());

Similarly, you need to configure the template rendering engine as ejs.

app.set("view engine", "html");app.set("views", __dirname + '/views');app.register("html", ejs);

Let's take a look at one of the most complex templates in views/index.html,
What I like most about ejs is that it is easy to understand without having to learn a set of syntaxes.

URL Routing and Controllers

The controller processes an http request. express provides a simple routing method.

app.get('/', todo.index);app.post('/todo/new', todo.new);app.get('/todo/:id', todo.view);app.get('/todo/:id/edit', todo.edit);app.post('/todo/:id/edit', todo.save);app.get('/todo/:id/delete', todo.delete);app.get('/todo/:id/finish', todo.finish);

For example, if you add a todo record: http get/new, it will be processed by the todo. new method,
The Code logic includes the verification of the title parameter validity, saving data to the database, and http redirect:

Exports. new = function (req, res, next) {var title = req. body. title | ''; title = title. trim (); if (! Title) {return res. render ('error', {message: 'title is required '});} db. query ('insert into todo set title = ?, Post_date = now () ', [title], function (err, result) {if (err) return next (err); res. redirect ('/');});};

For more controller processing logic, see/controllers/todo. js.

Database initialization and startup of Web Processes
$ mysql xxx$ source ~/todo/todo.sql$ node server.js
Love

We can see that, whether it is python web development or nodejs, our previous web concepts still do not need to be changed.
These are some of our familiar keywords:
Http, request, response, html, template engine, url routing, MVC, GET, POST, MYSQL, Database...

The difference is that only javascript can drive everything above.

Hope this article will be useful to you ^_^

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.