A Chatroom for all! Part 1-introduction to node. JS (forwarded)

Source: Internet
Author: User
Tags install node

The project team used node. JS and found this article to be good. Forward, please. Original address: "Original".

-------------------------------------------

A Chatroom for all! Part 1-introduction to node. js

Rami Sayar

4 SEP

    • 7

This node. JS Tutorial Series would help you build a node. JS powered real-time chatroom web App fully deployed in the CL Oud. You is expected to know HTML5 and JavaScript. Throughout the series, you'll learn how to setup node. js on Your Windows machine, how to develop a Web Frontend with Express, how to deploy a node Express apps to Azure, how to use socketio to add a real-time layer and how to deploy it all together.

Level:beginner to Intermediate.

Part 1-introduction to node. js

Part 2-welcome to Express with node. JS and Azure

Part 3-building a backend with Node, Mongo and Socket.io

Part 4–building a chatroom UI with Bootstrap

Part 5-connecting the chatroom with WebSockets

Part 6–the Finale and debugging Remote Node Apps

Part 1-introduction to node. js

Welcome to Part 1 of the node. JS Tutorial series:a chatroom for all! In this part, I'll explain what node. js is, why do you should pay attention to node. js and how to setup your machine.

What is node? Why node?

node. js is a runtime environment and library for running JavaScript applications outside the browser. node. js is mostly used to run real-time server applications and shines through its performance using non-blocking I/O and Asynchronous events. A complete Web ecosystem have been built around node. JS with several web app frameworks and protocol implementations Availa ble for usage. It's definitely one of the easiest and fastest the develop real-time applications on the web today.

Why use node? One word answer:javascript. JavaScript is a extremely popular language and is credited with being one of the driving forces this turned the web into The dynamic Wonderland it is today. What are can do in a browser nowadays, rivals all others!

JavaScript arose on the frontend but-thanks to the V8 JavaScript engine and the work of Ryan Dahl-you can now run NETW Orked JavaScript applications Outside of the browser precisely to build Web apps. node. js lets you unify the programming language used by your app-no longer does you need a different language for your BAC Kend, you can use JavaScript throughout. If your background is in building and design websites and web apps frontends in HTML, CSS and JavaScript, you don ' t need to Pick up another language to develop complex Data-driven back-ends for your apps.

node. JS plays a critical role in the advancement of WebSockets as a method for real-time communication between the front a nd back ends. The use of WebSockets and the Libraries building on that protocol such as Socket.io has really pushed what's expected of Web applications and lets us developers explore new ways to create the web.

Setting up node. js on Windows 8

To get started, you'll need a reasonably up to date machine, I'll be the showing you do to install node. js on Windows 8.1 .

Firstly, you'll need to download and install the node. JS Runtime. You can download the current version 0.10.30 (as of this writing) here:http://nodejs.org/download/. Choosing the Windows Installer is one of the easiest ways to get started.

Alternatively, if you is a fan of chocolatey, the package Manager for Windows, you can install node by running:

Choco Install Nodejs.install

You should double check that the node executable have been added to your PATH system environment variable. Watch This video, if you want to the change your environment variables on Windows 8 and Windows 8.1. You'll want to make sure the following folder have been added to the PATH variable:

C:\Program Files (x86) \nodejs\

If you go to your Command Prompt and type in node–h, you should see the Help documentation for the node executable displa Yed.

Along with node. js, NPM, the system used to manage Node packages, should is installed and available on the Command Prompt as well. Simply type in npm–h, you should see the Help documentation for NPM displayed.

If you encounter a error similar to this one:

Error:enoent, stat ' C:\USERS\SOMEUSER\APPDATA\ROAMING\NPM '

The resolution is to create a folder at the path specified above, as shown on this stackoverflow question. This is a issue in the latest node. JS installer and should be resolved by next release. You can create the folder like so:

Mkdir-r C:\USERS\SOMEUSER\APPDATA\ROAMING\NPM

Why use Visual Studio 2013? Node Tools for Visual studio!

With node. js installed, it ' s time to select a development tool. Of course, you is free to use any editing tool want and why use a glorified notepad when you can experience the full Power of Enterprise-grade integrated development environments like Visual Studio. You get the IT for free to boot!

Now what's cool about Node Tools for Visual Studio was that's adds node. JS support for editing, Intellisense, performance Profiling, NPM, TypeScript, debugging locally and remotely (including on Windows/macos/linux), as well Azure Web Sites and Cloud Service.

Throughout these tutorials, I'll be using Visual Studio-to-develop, debug and deploy the chat engine, you are Welco development tool you wish. If you want to use Visual Studio, can download any of the following editions of Visual Studio and then install the FRE E Node Tools for Visual Studio.

· [ This one was free] Visual Studio Express for Web (requires Update 2). Download here.

· Visual Studio 2 Pro or higher (requires Update)

· Visual Studio 4 Pro or higher (requires Update)

Don ' t forget to install the free Node Tools for Visual Studio.

Starting a new node. js Project in Visual Studio

Note:screenshots show Visual Studio Ultimate.

Starting a new node. JS project is fairly straight forward.

1. You want to boot Visual Studio and go to the File > New > Project menu item.

2. You'll want to go-installed > Templates > JavaScript menu item on the left and select Blank node. js Web Appl Ication on the right. Choose a location and name for your project and press OK.

3. You'll be presented with the following-screen, feel-explore Visual Studio at the. You'll want to open the generated server.js "file in the solution Explorer" on the right typically and may be located ELS Ewhere on your screen.)

4. You can now debug your node Web application in your preferred browser.

Hello World in node. js

As is typical on other languages, the generated code example shows the "Hello World" in the browser. Let me explain how the generated code in Server.js works on line. *note:as stated in this tutorial series description, I am assuming are a knowledge of JavaScript, HTML5 and how Http/the Internet work.

Line 1

var http = require (' http ');

node. JS has a simple module and dependencies loading system. You simply call the function "require" with the path of the file or directory containing the module you would like to load At which point a variable are returned containing all the exported functions of this module.

Line 2

var port = Process.env.port | | 1337;

On this line, we want to determine on which port the HTTP server serving the HTML should run. If a port number is specified in the environment variables, we'll use that one or we'll simply use 1337.

Line 3

Http.createserver (function (req, res) {

We want to create a server to handle HTTP requests. We'll also pass the Createserver function a function callback containing, parameters to a handle each individual requ EST and return a response. Take a look at the Michael Vollmer ' sarticle if you ' ve never encountered callback functions in JavaScript. The request received is passed in the Req parameter and the response are expected to written to the res parameter.

Line 4

Res.writehead ($, {' Content-type ': ' Text/plain '});

Any HTTP response requires a status-line and headers, to learn more on HTTP headers and how they work check out this AR Ticle. In this case, we want to return with the status response and to specify the Content-type as plain text. We specify the calling the Writehead function on the response object.

Line 5

Res.end (' Hello world\n ');

Once we are doing writing the response we want to the end function. We can also pass the final content through the End function, in this case we want to send the string ' Hello world ' in Plai N Text.

Line 6

}). Listen (port);

We close off the callback and call the function listen at the port we defined earlier, this would start the server and star T accepting requests sent to the defined port.

To see the result, you can start debugging by pressing on the button shown in the previous screenshot. You can see ' Hello world ' in the browser.

voila! You has now successfully run a node. js app on Windows 8.1 using Visual Studio 2013.

Stay tuned!

Stay tuned for part 2–how-to-Deploy Your Hello World into the Cloud to being released in the next week. You can stay the date by following my Twitter account @ramisayar.

A chatroom for all! Part 1-introduction to node. JS (forwarded)

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.