-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodejs.html
197 lines (162 loc) · 10.4 KB
/
nodejs.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!--Stylesheet-->
<link rel="stylesheet" href="styles.css">
<title>Techopedia - Node.JS</title>
</head>
<body>
<div class="container" style="width: auto;">
<div class="mainDiv">
<!--Navigation pane -->
<nav class="navbar navbar-expand-md navbar-light">
<a class="logo" href="index.html">TECHOPEDIA</a>
<div class="container">
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="Bootstrap.html">Bootstrap</a>
</li>
<li class="nav-item">
<a class="nav-link" href="markdown.html">Markdown</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="nodejs.html">Node.JS</a>
</li>
<li class="nav-item">
<a class="nav-link" href="jquery.html">jQuery</a>
</li>
</ul>
</div>
</div>
</nav>
<hr class="m5">
<!--Topic Title Logo -->
<div class="container">
<img class="topicLogo img-fluid" src="nodejs.png" alt="nodejs logo">
</div>
<div>
<!--Content of the page starts here-->
<hr class="m5">
<section>
<h4>INTRODUCTION</h4>
<p>Node.js is an open source, cross-platform runtime environment for developing server-side and networking applications. <br> Node.js applications are written in JavaScript, and can be run within the Node.js runtime on OS X, Microsoft Windows, and Linux.</p>
<p>Simply ---> <em>Node.js = Runtime Environment + JavaScript Library</em></p>
<p>You would find the simple basics of the Node.js scripting language below...</p>
<br/>
<p class="subtitle"><a class="links" href="https://rextester.com/l/nodejs_online_compiler" target="_blank"> Click Here for an online Node.js compiler</a></p>
</section>
<hr class="m5">
<section>
<article>
<h5>1. REQUIRED ENVIRONMENT</h5>
<p>To set up a local environment, the following two softwares are required:
<ol type="i">
<li><b><em>Text Editor</em></b> - Any type of text editor that is used to type a program. The source file need to be saved with a <b>.js</b> which is the Node.js extension.</li>
<li><b><em>Node.js binary installables</em></b> - The Node.js installable archive file can be downloaded from <a href="https://nodejs.org/download/" target="_blank">Node.js Downloads</a> and installed appropriately according to the respective OS. This is required so that the Node.js interpretor can interpret the javascript source file as required. <br/><br/>
<aside>
<div class="tipicon"><img src="tipicon.jpg" class="tipicon"></div><br/><br/>
<p><em><b><font id="redfont">TIP:</font></b></em></p>
<p>NPM is a package manager for Node.js packages or modules. This program is installed in your computer when Node.js is installed. Therefore, packages can be easily downloaded by telling NPM to download the required package in a command line interface. For example:<br/>
C:\Users\Your Name>npm install upper-case<br/>
Once the module is installed, it can be imported directly into any node.js file.
</p>
</aside>
</li>
</ol>
</p>
</article>
<br/><br/>
<hr>
<article>
<h5>2. STEP BY STEP APPROACH TO CREATE A SIMPLE APPLICATION</h5>
<p>
<ol type="i">
<li><b><em>Import required modules</em></b> - use the require directive to load the http module and store the returned HTTP instance into an http variable. <br/><div class="codes">var http = require("http");</div><br/>
<aside>
Just as modules can be imported using the <b>require</b> function, your own modules can be created and included in the application. Firstly, the module needs to be saved into a .js module file using an <b>export</b> function and then imported into any of your node.js files. The following is an example of creating a module that returns the current date and time:<br/>
<div class="codes">exports.myDateTime = <font id="bluefont">function</font> () {<br/>
<font id="bluefont">return</font> Date();<br/>
};<br/></div><br/>
</aside>
</li>
<br/>
<li><b><em>Create server</em></b> - use the created http instance and call http.createServer() method to create a server instance and then we bind it at port 8081 using the listen method associated with the server instance. Pass it a function with parameters request and response. Write the sample implementation.<br/><div class="codes">http.createServer(<font id="bluefont">function</font> (request, response) {<br>
<font id="redfont">// Send the HTTP header <br>
// HTTP Status: 200 : OK <br>
// Content Type: text/plain <br></font>
response.writeHead(<font id="bluefont">200</font>, {'<font id="greenfont">Content-Type': 'text/plain</font>'});<br>
<font id="redfont">// Send the response body as "Hello World"</font>response.<font id="bluefont">end</font>('<font id="greenfont">Hello World\n</font>');<br>
}).listen(8081); <font id="redfont">// Console will print the message<br></font>
console.log('<font id="greenfont">Server running at http://127.0.0.1:8081/</font>');<br></div></li>
<br/>
<li><b><em>Testing request and response</em></b> - put step 1 and step 2 together in a source file with a <b>.js</b> extension, execute the source file, open the respective server url in any web browser and observe the result. </li>
</ol>
</p>
<p></p>
</article>
<hr>
<article>
<h5>3. OTHER BASIC IMPORTANT SYNTAXES</h5>
<p>
<ol type="i">
<li><b><em>File System</em></b> - use the <b>require()</b> method to include the file system module.<br/>
<div class="codes">var fs = require("fs");</div>
It is used to do the following tasks:<br/>
<ol type="a" class="subol">
<li>Read files - Reads files in your computer using the <font id="redfont">fs.readFile()</font> method</li>
<li>Create or Update files - Create new files or append to existing files in your computer using the <font id="redfont">fs.appendFile()</font>, <font id="redfont">fs.open()</font> or <font id="redfont">fs.writeFile()</font> methods</li>
<li>Delete files - Delete files that have a File System module using the <font id="redfont">fs.unlink()</font> method</li>
<li>Rename files - Rename files that have a File System module using the <font id="redfont">fs.rename()</font> method</li>
<li>Upload files - Upload files using the 'Formidable' module. First, write a node.js file with an upload field. Import the Formidable module to be able to parse the uploaded file once it reaches the server, so that the file gets placed on a temporary folder on your computer. Finally, find the path directory of the saved file using the <font id="redfont">parse()</font> method and move it to your destined folder. </li><br/>
</ol>
</li>
<li><b><em>URL Module</em></b> - using this built-in module, the URL of a website can be split into readable parts. For example, <br/>
<div class="codes"><font id="bluefont">var</font> url = require('<font id="redfont">url</font>');<br/>
<font id="bluefont">var</font> adr = '<font id="redfont">http://localhost:8080/default.htm?year=2017<font id="greenfont2"></font>&month=february</font>'<br/>
<font id="bluefont">var</font> q = url.parse(adr, <font id="bluefont">true</font>);<br/>
console.log(q.host); <font id="greenfont">//returns 'localhost:8080'</font><br/>
console.log(q.pathname); <font id="greenfont">//returns '/default.htm' </font><br/>
console.log(q.search); <font id="greenfont">//returns '?year=2017&month=february' </font><br/>
<font id="bluefont">var</font> qdata = q.query; <font id="greenfont">//returns an object: { year: 2017, month: 'february' }</font><br/>
console.log(qdata.month); <font id="greenfont">//returns 'february'</font><br/></div>
</li><br/>
<li><b><em>Email Module</em></b> - using the 'Nodemailer' built-in module, e-mails can be sent from your server.<br/>
</li>
</ol>
</p>
</article>
</section>
<div class="divAside">
<aside>
</aside>
</div>
</div>
</div>
</div>
<br/>
<hr>
<!-- Footer -->
<footer align="center">
<div class="container">© Design by: David, John, Steven and Shazna
</div>
</footer>
<!--Bootstrap Javascript-->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>