NODEJS

Node.js Modules

Node.js Modules

What is a Module in Node.js? Consider modules to be the same as JavaScript libraries. A set of functions you want to include in your application. Built-in Modules Node.js has a set of built-in modules which you can use without any further installation. Look at our Built-in Modules Reference for a complete list of modules. Include Modules To include a module, use the require() function with the name of the module: var http = require('http'); Now your application has access to the HTTP module, and is able to create a server: http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!');…
Read More
Node.js File System

Node.js File System

Node.js as a File Server The Node.js file system module allows you to work with the file system on your computer. To include the File System module, use the require() method: var fs = require('fs'); Common use for the File System module: Read files Create files Update files Delete files Rename files Read Files The fs.readFile() method is used to read files on your computer. Assume we have the following HTML file (located in the same folder as Node.js): demofile1.html My Header Create a Node.js file that reads the HTML file, and return the content: Example var http = require('http');…
Read More
Node.js URL Module

Node.js URL Module

The Built-in URL Module The URL module splits up a web address into readable parts. To include the URL module, use the require() method: var url = require('url'); Parse an address with the url.parse() method, and it will return a URL object with each part of the address as properties: Example Split a web address into readable parts: var url = require('url'); var adr = 'http://localhost:8080/default.htm?year=2017&month=february'; var q = url.parse(adr, true); console.log(q.host); //returns 'localhost:8080' console.log(q.pathname); //returns '/default.htm' console.log(q.search); //returns '?year=2017&month=february' var qdata = q.query; //returns an object: { year: 2017, month: 'february' } console.log(qdata.month); //returns 'february' Run example » Node.js…
Read More
Node.js Email

Node.js Email

The Nodemailer Module The Nodemailer module makes it easy to send emails from your computer. The Nodemailer module can be downloaded and installed using npm: C:UsersYour Name>npm install nodemailer After you have downloaded the Nodemailer module, you can include the module in any application: var nodemailer = require('nodemailer'); Send an Email Now you are ready to send emails from your server. Use the username and password from your selected email provider to send an email. This tutorial will show you how to use your Gmail account to send an email: Example var nodemailer = require('nodemailer'); var transporter = nodemailer.createTransport({ service:…
Read More
MySQL Get Started

MySQL Get Started

  Node.js can be used in database applications. One of the most popular databases is MySQL. MySQL Database To be able to experiment with the code examples, you should have MySQL installed on your computer. You can download a free MySQL database at https://www.mysql.com/downloads/. Install MySQL Driver Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the "mysql" module, downloaded from NPM. To download and install the "mysql" module, open the Command Terminal and execute the…
Read More
MySQL Create Database

MySQL Create Database

Creating a Database To create a database in MySQL, use the "CREATE DATABASE" statement: Example Create a database named "mydb": var mysql = require('mysql');var con = mysql.createConnection({ host: "localhost", user: "yourusername", password: "yourpassword" }); con.connect(function(err) { if (err) throw err; console.log("Connected!"); con.query("CREATE DATABASE mydb", function (err, result) { if (err) throw err; console.log("Database created"); }); }); Run example » Save the code above in a file called "demo_create_db.js" and run the file: Run "demo_create_db.js" C:UsersYour Name>node demo_create_db.js Which will give you this result: Connected! Database created THU HUYỀN - LINKPIZ.COM
Read More
Node.js Intro

Node.js Intro

What is Node.js? Node.js is an open source server environment Node.js is free Node.js runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.) Node.js uses JavaScript on the server Why Node.js? Node.js uses asynchronous programming! A common task for a web server can be to open a file on the server and return the content to the client. Here is how PHP or ASP handles a file request: Sends the task to the computer's file system. Waits while the file system opens and reads the file. Returns the content to the client. Ready to handle the next request.…
Read More
Node.js Get Started

Node.js Get Started

Download Node.js The official Node.js website has installation instructions for Node.js: https://nodejs.org Getting Started Once you have downloaded and installed Node.js on your computer, let's try to display "Hello World" in a web browser. Create a Node.js file named "myfirst.js", and add the following code: myfirst.js var http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/html'}); res.end('Hello World!'); }).listen(8080); Save the file on your computer: C:UsersYour Namemyfirst.js The code tells the computer to write "Hello World!" if anyone (e.g. a web browser) tries to access your computer on port 8080. For now, you do not have to understand the code.…
Read More

Node.js Events

Node.js is perfect for event-driven applications. Events in Node.js Every action on a computer is an event. Like when a connection is made or a file is opened. Objects in Node.js can fire events, like the readStream object fires events when opening and closing a file: Example var fs = require('fs'); var rs = fs.createReadStream('./demofile.txt'); rs.on('open', function () { console.log('The file is open'); }); Events Module Node.js has a built-in module, called "Events", where you can create-, fire-, and listen for- your own events. To include the built-in Events module use the require() method. In addition, all event properties and…
Read More
Node.js NPM

Node.js NPM

What is NPM? NPM is a package manager for Node.js packages, or modules if you like. www.npmjs.com hosts thousands of free packages to download and use. The NPM program is installed on your computer when you install Node.js NPM is already ready to run on your computer! What is a Package? A package in Node.js contains all the files you need for a module. Modules are JavaScript libraries you can include in your project. Download a Package Downloading a package is very easy. Open the command line interface and tell NPM to download the package you want. I want to…
Read More
No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.