Simple web server function example implemented by nodejs, nodejsweb

Source: Internet
Author: User

Simple web server function example implemented by nodejs, nodejsweb

This article describes the simple web server functions implemented by nodejs. We will share this with you for your reference. The details are as follows:

Front-end js Code:

<Script src = "jquery-1.12.1.js"> </script> <script type = "text/javascript"> $. ajax ({url: 'http: // 192.168.0.114: 8080', // server address type: 'post', timeout: 30, dataType: 'json', data: {"username": username, "password": password}, success: function (res) {console. log (JSON. stringify (res);}, err: function (err) {alert (err );}});

Nodejs code:

Var http = require ('http'); var url = require ('url'); var qs = require ('querystring'); http. createServer (function (req, res) {res. writeHead (200, {'content-type': 'text/plain; charset = UTF-8 ', 'access-Control-Allow-origin ':'*'}); if (req. method. toUpperCase () = 'get') {var query = url. parse (req. url, true ). query; // the received data res. end (JSON. stringify (query);} else if (req. method. toUpperCase () = 'post') {var postData = ''; req. on ('data', function (data) {postData + = data; // accepted data}); req. on ('end', function () {var query = qs. parse (postData); res. end (JSON. stringify (query); // returned data });}}). listen ('123', function () {console. log ('this is callback');}); console. log ('server is running ');

I hope this article will help you design nodejs programs.

Related Article

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.