WIN7 Nodejs framework Express use and database public operation class collation

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

Specific steps:

1, installation and development tools Webstorm;

2, install NODE/NPM (: https://nodejs.org/download/) Choose suitable for your xxx.mis installation;

3. Install Express frame (CMD window mode, execute with npm command: NPM install Express);

4, create nodejsexpress project, use Ejs template;

5. Download Node-mssql Connection Database driver (enter the specified directory with npm command: NPM install node-mssql);

6, copy Node-mssql folder to express project Node-modules directory;

7, using DBHelper tool class, data query, and Ejs page data display;

The DBHelper code is as follows:

varNode_mssql = require (' Node-mssql '));/*add configuration to query object*/varQueryobj =NewNode_mssql. Query ({host:' 192.168.20.135 ', Port:1433, Username:' Sa ', Password:' Sa ', DB:' Database '});varInsert =function(data, inserttable, callback, res) {queryobj.table (inserttable);    Queryobj.data (data); Queryobj.insert (function(results) {//SuccessCallback (RES, "add success! "); }, function(err, sql) {if(ERR) {//ErrorCallback (res, "Add failed! ");        Console.log (ERR); }    });};varList =function(Wheresql, table, callback, res) {queryobj.table (table);    Queryobj.where (Wheresql); Queryobj.select (function(data) {//SuccessCallback (data, RES); }, function(err, sql) {if(ERR) {//ErrorConsole.log (ERR); }    });};varUpdate =function(data, option, Uptable, callback, res) {queryobj.table (uptable);    Queryobj.data (data);    Queryobj.where (option); Queryobj.update (function(results) {//Success CallbackCallback (RES, "modified successfully! "); }, function(err, sql) {if(Err) {callback (res,"The 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 the action filter, code

After this configuration, the access address: Http://xxxx/list is distributed to the List.js controller, and then the list.js to process the code, LIST.EJS for the note display,

The List.js code is as follows:

varExpress = require (' Express ');varDBHelper = require ('./dbhelper.js '));varRouter =Express. Router ();/*GET home page.*/Router.get (‘/‘,function(req, res, next) {Dbhelper.list ({},' Dbo.table1 ', callback, RES); //The list parameter, the first one is the wheresql query condition, the JSON format, the second is the table name, the third is the callback function, and the fourth is the response class that express returns the client});varcallback =function(data, res) {Res.render (' List ', {listdata:data}); //first parameter: The template name corresponds to List.ejs, the second is the parameter name and the data};module.exports= Router;

The List.ejs code is as follows:

<! DOCTYPE html>(var i = 0; i < Listdata.length; i++) {%>    <li><%= listdata[i]. Id%>: <%= listdata[i]. Name%></li>    <%}%></ul></body>

Execution results

WIN7 Nodejs framework Express use and database public operation class collation

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.