Use the Nodejs Express framework to build a server on this computer

Source: Internet
Author: User
Tags node server python script

[This article is from the Sky Cloud-owned blog Park]

Brief introduction

Use the express framework to build a server on this machine so that you can run the appropriate functionality on your server with the specified URL.

Express is a NODEJS-based framework that we can use to complete the service-side authoring.

The following Mocker file structure I have uploaded to GitHub.

Installation

First install Nodejs and NPM, these two things are a bit like Python and pip relationship, Nodejs is a JS package, and NPM can install some libraries for Nodejs.

With the Nodejs after installing Express.

Then create a local folder, as the node server is located in the folder, I named Mocker. Install click here to view tutorials.

After that, a static folder is created in the Mocker folder for use in the passive resource file.

Then create a scripts folder to put my Python script.

My idea is that the user accesses my server my local nodejs can invoke a Python script to accomplish some tasks and then present the results to the user.

My node script is called Mocker.js, which reads as follows:

functionSleep (sleeptime) { for(varStart = +NewDate; +NewDate-start <=sleeptime;) { } }functionExec_cmd (cmdstr) {varexec = require (' child_process '). exec; EXEC (CMDSTR,function(err,stdout,stderr) {if(Err) {Console.log (' Error: ' +stderr); } Else{console.log (stdout); }    });}varExpress = require (' Express ');varApp =Express ();varPath = require (' path '); Console.log (Path.join (__dirname,‘/‘) ; App.use (Express.static (Path.join (__dirname,‘/‘)));varDistribution = '/static/html/distribution.html 'App.get ("/distribution",function(req, res) {varcmdstr = ' python scripts/check_tasks_distribution.py ';    Exec_cmd (CMDSTR); varFS = require ("FS"); Fs.exists (Path.join (' Static ', ' HTML ', ' distribution.html '),function(exists) {if(exists) {res.redirect (distribution); } Else{exec_cmd (CMDSTR); Sleep (1000); }    });});varServer = App.listen (3000,function () {    varHost =server.address (). Address; varPort =server.address (). Port; Console.log (' Listening at http://%s:%s ', host, Port);});

The main idea is that when users access distribution this route will be on my server through the command line to execute the Scripts folder under the Check_tasks_ distribution.py the script and then opens the generated static resource file Distribution.html page. This script I used to write a statistical function about the Jira task that everyone in the statistics group is working on.

Start

Execute the command line under the Mocker path to start the Mocker.js server file:

You can then access the list of Jira tasks in progress in the browser via Http://server-ip:3000/distribution.

Inspired

We can integrate scripts that are usually written to help our daily work to this server, so that we can display the script and tools platform to colleagues for sharing purposes.

Use the Nodejs Express framework to build a server on this computer

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.