Win7 under the Nodejs+express Tutorial and database operation class collation

Source: Internet
Author: User
Tags mssql install node npm install node

First, let's take a look at the steps to install Nodejs and Frame Express


Download the installation file from node website, website address: http://nodejs.org/


This installer is also very conventional, sequential Click Next can be, or make some simple choice, needless to say, just to emphasize a point, one of the steps as shown: ADD to Path must be selected. The effect of this is simply, in the Windows command line to add node-related commands, details please own Baidu.


We were very happy to have node installed, and then installing Express,express is the only web framework that node officially recommended, providing a number of basic and convenient features.

Perform "NPM Install-g Express" on the command line waiting for downloading and completing the installation automatically. Test Express to complete the installation of a method is to see its version number, execute the command "express-v" normal situation next output version number as shown in the picture, but encountered abnormal, will prompt "Express is not internal or external command", so the question comes, this is what reason?


There are two possibilities: ① to install node in the second step does not add environment variables, which resolves the environment variables added by node. ②express 4.x version of the command tool out, you need to install a command tool, execute the command "NPM install-g Express-generator" After the completion of the test.


Install development tools Webstorm, this is relatively simple, we will not detail.


Create Nodejsexpress project, use Ejs template;

Download Node-mssql Connection Database driver (go to specified directory with npm command to execute: NPM install node-mssql);

Copy the Node-mssql folder to the Express project Node-modules directory;

Use the DBHelper tool class, data query, and Ejs page data display;

The DBHelper code is as follows:

Var node_mssql = require (' Node-mssql '); /* add configuration to query object */var queryobj = new  Node_mssql. Query ({    host:  ' 192.168.20.135 ',     port: 1433,      username:  ' sa ',     password:  ' sa ',     db:
  ' database '}); var insert = function  (data, inserttable, callback, res)  {  
  queryobj.table (inserttable);
    queryobj.data (data);     queryobj.insert (function  (results)  {         //success         callback (res, ) Add success!
");     }, function  (err, sql)  {         if  (err)  {//error       &Nbsp;     callback (res,  "Add failed!")
");
            console.log (ERR);
        }     });
}; var list = function  (wheresql, table, callback, res)  {  
  queryobj.table (table);
    queryobj.where (Wheresql);     queryobj.select (function  (data)  {       
 //success         callback (data, res);     }, function  (err, sql)  {         if  (err)  { //error            
 console.log (ERR);
        }     });
}; var update = function  (DATA,&NBSp;option, uptable, callback, res)  {    queryobj.table (upTable);  
   queryobj.data (data);
    queryobj.where (option);     queryobj.update (function  (results)  {         //  success callback         callback (Res,   "Modified successfully!"
");     }, function  (err, sql)  {         if  (Err)  {            callback (res ,  "Modification failed!"
");
            console.log (ERR);
        }     });
};
exports.insert = insert;
exports.list = list; exports.update = update;


To use DBHelper, show in the list page, first configure App.js, set action filtering, code as shown:




After this configuration, access address: http://xxxx/list distributed to the List.js controller, and then in the List.js processing code, LIST.EJS to carry out the notes show,

The List.js code is as follows:

var express = require (' Express ');
var dbhelper = require ('./dbhelper.js ');
var router = Express. Router ();
/* Get home page.    *
/Router.get ('/', function (req, res, next) {
dbhelper.list ({}, ' Dbo.table1 ', callback, RES);
List parameters, the first is Wheresql query conditions, JSON format, the second is the table name, the third is the callback function, the fourth is the Express return client response Class
});
var callback = function (data, res) {
res.render (' list ', {listdata:data});
First parameter: template name corresponds to List.ejs, second is parameter name and data
};
Module.exports = router;




The List.ejs code is as follows:

<! DOCTYPE html>

Implementation results as shown:




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.