Node. js implements simple interface servers

Source: Internet
Author: User
Tags node server

Node. js implements simple interface servers

Use node. js to implement interface server functions. Main features:

1) You do not need to restart the added interface.
2) asynchronous execution, but the interface reading is Synchronous Code (from top to bottom), or can be parallel and serial as needed
This is just to throw the basic idea, so GET and no encryption are used.
First, start the listening port and configure the access rules. (Dynamically execute corresponding Interface scripts by identifying specific URLs)

-----
| ---- HamstrerServlet
| ------ Command3G
| ------ Login. js // login script (this is just a simple demonstration)
| --- Server. js (main STARTUP script)
| --- Dbutil (Database Operations)

Server. js

Var $ = require ('jquery ');
Var _ = require ('underscore ');
Var vm = require ('vm ');
Var fs = require ('fs ');
Var journey = require ('Journey ');
Var async = require ('async ');
Var dbutil = require ('./dbutil ');

String. prototype. replaceAll = function (s1, s2 ){
Var demo = this
While (demo. indexOf (s1 )! =-1)
Demo = demo. replace (s1, s2 );
Return demo;
}
// Create a Router
Var router = new (journey. Router );

// Create the routing table
Router. map (function (){
// This. root. bind (function (req, res) {res. send ("Welcome ")});
This. get (/HamstrerServlet \/(\ w * \ W * \ w *) */). bind (function (req, res, id ){
Var runJsPath = this. request. url. pathname. replaceAll ("/HamstrerServlet", "") + ". js ";
Console. log ("executed script file:" + runJsPath );

// Input Bind Variable
Var sandbox = {
Req: req,
Res: res,
$: $,
Dbutil: dbutil,
Async: async,
Console: console
};

Fs. readFile ('./HamstrerServlet' + runJsPath, function (err, data ){
Vm. runInNewContext (data, sandbox, 'myfile. vm ');
});

});
This. post ('/^ HamstrerServlet \/(\ w *) $/)'). bind (function (req, res, data ){
Res. send (1, 200 );
});
});

Require ('http'). createServer (function (request, response ){
Var body = "";

Request. addListener ('data', function (chunk ){
Body + = chunk
});
Request. addListener ('end', function (){
Router. handle (request, body, function (result ){
Response. writeHead (result. status, result. headers );
Response. end (result. body );
});
});
}). Listen (8080 );

Dbutil. js

Var mysql = require ('mysql'); // import the mysql Module

Var pool = mysql. createPool ({
Host: '1970. 168.140.237 ',
User: 'root ',
Password: '000000 ',
Database: 'command3g'
});

// Query SQL statements
Function query (strSQL, param, callback ){
Pool. getConnection (function (err, connection ){
Connection. query (strSQL, param, function (err, rows, fields ){
If (err) throw err;
Callback (rows, fields );
Connection. end ();
// Connection. destroy ();
});
});
}

Exports. query = query;

After node server. js is started, access http: // localhost: 8080/HamstrerServlet/command3G/login

The output is as follows:

At this time, modifications to login. js will take effect immediately and do not need to restart the server.

You may also like the following content about Node. js:

Install and configure Node. js v4.0.0 on Ubuntu 14.04/15.04

How to install Node. js in CentOS 7

Build a Node. js development environment in Ubuntu 14.04

Install and configure the Node. js development environment in javasru 12.04

Getting started with Node. Js [PDF + related Code]

Node. js Development Guide hd pdf Chinese version + source code

Node. js getting started Development Guide

Compile and install Node. js in Ubuntu

Node. js details: click here
Node. js: click here

This article permanently updates the link address:

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.