Nodejs Getting Started learning

Source: Internet
Author: User

Basis

node. js is a JavaScript runtime environment written in C + + that features: event-driven, asynchronous i/0. As a server-side JavaScript interpreter, node is a lightweight and efficient development platform that allows users to build responsive, highly scalable Web applications that use event-driven and non-blocking I/O models and are ideal for developing distributed applications that are data-intensive and demand high real-time response.

Server.js//Simple HTTP Server

var http = require (' http '); http.createserver(function(req, res) {    Res.writehead ( {' Content-type ': ' Text/plain '});    Res.end (' Hello world\n ');}). Listen (8888); Console.log (' server running at http://127.0.0.1 ');

Execution: Node Server.js

Module

Main.js

var hello = require ('./hello '); Hello.world ();

Hello.js

function () {    console.log (' Hello World ');}

Main1.js

var Hello = require ('./hello1 'new  Hello (); Hello.setname (' AAA '); Hello.sayhello ();

Hello1.js

function Hello () {    var  name;      This function (myname) {        = myname;    }      This function () {        console.log (' hello ' + name);     = Hello;
Event

Event.js

var Eventemitter = require (' Events '). Eventemitter; var New Eventemitter (); Event.on (function() {    console.log (' event1 occured. ') );}); SetTimeout (function() {    event.emit (' event1 '1000);

Event1.js

var events = require (' Events '); var New events. Eventemitter (); Emitter.on (function(arg1, arg2) {    console.log (' listener1 ', arg1, ARG2);}); Emitter.on (function(arg1, arg2) {    console.log (' Listener2 ', arg1, arg2);}); Emitter.emit (' event1 ', ' AAA ', 123);

More reference http://www.w3cschool.cc/nodejs/nodejs-event.html

Routing

Index.js

var server = require ('./server '); var router = require ('./router '); Server.start (Router.route);

Server.js

varHTTP = require (' http ');varurl = require (' URL '));functionStart (route) {functiononrequest (req, res) {varpathname =Url.parse (req.url). Pathname; Console.log (' Request for ' + pathname + ' received. ');        Route (pathname); Res.writehead ($, {' Conten-type ': ' Text/plain '}); Res.write (' Hello, World ');    Res.end (); } http.createserver (ONrequest). Listen (8888); Console.log (' Server has started. ');} Exports.start= start;

Router.js

function Route (pathname) {    console.log (' about-route a request for ' += route;

Nodejs Getting Started learning

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.