Node. js implements simple interface server instance code, node. js instance

Source: Internet
Author: User
Tags node server

Node. js implements simple interface server instance code, node. js instance

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 Operation)

Server. js

Var $ = require ('jquery '); var _ = require ('underscore'); var vm = require ('vm '); var fs = require ('fs '); var journey = require ('journ'); 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 (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 (0, 8080 );

Dbutil. js

Var mysql = require ('mysql'); // import mysql Module var pool = mysql. createPool ({host: '2017. 168.140.237 ', user: 'root', password: '000000', database: 'command3g'}); // query the SQL statement 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;

Login. js

Console. log ("beigin"); // parallel processing, equivalent to two maps and then another ReReduce async. parallel ([function (callback) {// obtain the current time from the database dbutil. query ("select curtime () as date", null, function (rows, fields) {callback (null, rows [0]. DATE) ;}) ;}, function (callback) {// return a callback (null, 'Chinese test');}], function (err, results) {console. log (results); var retVal = {"currentTime": results [0], "desc": results [1]}; res. sendBody (JSON. stringify (retVal ));});

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.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.