Node.js


This draft deletes the entire topic.

expand all collapse all

Examples

  • 67

    First, install Node.js for your platform.

    Create an HTTP server listening on port 1337, which sends Hello, World! to the browser. Also, instead of using port 1337, you can use any port number of your choice which is currently not in use by any other service.

    The http module provides the functionality to create an HTTP server using the http.createServer() method. To create the application, create a file containing the following JavaScript code.

    const http = require('http');
    
    http.createServer((req, res) => {
    
        // 1. Tell the browser everything is OK (Status code 200), and the data is in plain text.
        res.writeHead(200, {
            'Content-Type': 'text/plain'
        });
    
        // 2. Send the announced text (the body of the page)
        res.end('Hello, World!\n');
    
    }).listen(1337);
    

    Save the file with any file name. In this case, if we name it hello.js we can run the application by going to the directory the file is in and using the following command:

    node hello.js
    

    The created server can then be accessed with the URL http://localhost:1337 or http://127.0.0.1:1337 in the browser.

    A simple web page will appear with a “Hello, World!” text at the top, as shown in the screenshot below.

    Screenshot

  • 22

    Node.js can also be used to create command line utilities. The code below reads a string from the command line argument to print a Hello message.

    To run this code:

    1. Create an empty file, and paste the code below. The name of the file isn't important but many people will name this file app.js or main.js.
    2. Change the permission of the file to make it an executable. Example chmod 700 FILE_NAME
    3. Run the app by typing ./APP_NAME
    #!/usr/bin/env node
    
    'use strict';
    
    /*
        The process.argv property returns an array containing the command line
        arguments passed when the Node.js process was launched. The first element
        will be process.execPath. The second element will be the path to the
        JavaScript file being executed. The remaining elements will be any
        additional command line arguments.
    
        Example: [ 'node', '/path/to/yourscript', 'arg1', 'arg2', ... ]
        URL: https://nodejs.org/api/process.html#process_process_argv
     */
    
    
    //    1. Extract the name of the app/file name.
    let appName = process.argv[1].split('/').pop();
    
    //    2. Save the first provided argument as the username.
    let name = process.argv[2];
    
    //    3. Check if the name was provided.
    if (!name) {
        //    1. Give the user an example how to use the app.
        console.log("Missing argument! \n\n\tExample: %s YOUR_NAME\n", appName);
    
        //    -> Exit the app if error. The nr 1 tells the system that the app quit
        //       with an error, thus another command won't be executed. For example:
        //       ./hello && ls -> won't execute ls
        //       ./hello David && ls -> will execute ls
        process.exit(1);
    }
    
    //    4. Display the message in the console.
    console.log('Hello, %s!', name);
    
  • 11

    The following example uses Express to create an HTTP server listening on port 3000, which responds with "Hello, World!".

    var express = require('express');
    var app = express();
    
    app.get('/', function (req, res) {
      res.send('Hello, World!');
    });
    
    app.set('port', (process.env.PORT || 3000));
    
    app.listen(app.get('port'), function () {
      console.log('Example app listening on port 3000!');
    });
    

    For more information about the Express framework, you can check the Web Apps With Express section

Please consider making a request to improve this example.

Remarks

Node.js is an event-based, non-blocking, asynchronous I/O framework that uses Google's V8 JavaScript engine. It is used for developing applications that make heavy use of the ability to run JavaScript both on the client, as well as on server side and therefore benefit from the re-usability of code and the lack of context switching. It is an open source. It supports cross platforms. Node JS applications are written in pure JavaScript It can be run within Node.js environment on WIndows, Linux, IBm etc…

Versions

VersionRelease Date
v7.2.02016-11-22
v7.1.02016-11-08
v7.0.02016-10-25
v6.9.12016-10-19
v6.9.02016-10-18
v6.8.12016-10-14
v6.8.02016-10-12
v6.7.02016-09-27
v6.6.02016-09-14
v6.5.02016-08-26
v6.4.02016-08-12
v6.3.12016-07-21
v6.3.02016-07-06
v6.2.22016-06-16
v6.2.12016-06-02
v6.2.02016-05-17
v6.1.02016-05-05
v6.0.02016-04-26
v5.12.02016-06-23
v5.11.12016-05-05
v5.11.02016-04-21
v5.10.12016-04-05
v5.102016-04-01
v5.92016-03-16
v5.82016-03-09
v5.72016-02-23
v5.62016-02-09
v5.52016-01-21
v5.42016-01-06
v5.32015-12-15
v5.22015-12-09
v5.12015-11-17
v5.02015-10-29
v4.42016-03-08
v4.32016-02-09
v4.22015-10-12
v4.12015-09-17
v4.02015-09-08
io.js v3.32015-09-02
io.js v3.22015-08-25
io.js v3.12015-08-19
io.js v3.02015-08-04
io.js v2.52015-07-28
io.js v2.42015-07-17
io.js v2.32015-06-13
io.js v2.22015-06-01
io.js v2.12015-05-24
io.js v2.02015-05-04
io.js v1.82015-04-21
io.js v1.72015-04-17
io.js v1.62015-03-20
io.js v1.52015-03-06
io.js v1.42015-02-27
io.js v1.32015-02-20
io.js v1.22015-02-11
io.js v1.12015-02-03
io.js v1.02015-01-14
v0.122016-02-09
v0.112013-03-28
v0.102013-03-11
v0.92012-07-20
v0.82012-06-22
v0.72012-01-17
v0.62011-11-04
v0.52011-08-26
v0.42011-08-26
v0.32011-08-26
v0.22011-08-26
v0.12011-08-26
Still have a question about Getting started with Node.js? Ask Question

Topic Outline