1.2 Hello You
Let's start with a simple Hello server. Follow the tasks below to create a simple Node server that outputs a greeting.
Task 1:
First, tell the response which status it should have (a successful status is200).
Task 2:
Next, write a message to the response body in the form of "Hello, this is <your name here>".
Task 3:
To finish it up, tell the response to end so the client on the other side knows it has received all the data.
```js var http = require('http'); http.createServer(function(request, response) { response.writeHead(200); response.write("Hello, this is Cain."); response.end(); }).listen(8080); ```
1.3 Convert Blocking
Not everyone knows why it's important to write non-blocking programs in Node.js. One of these unfortunate souls has written some code to read a file off the file-system using the blocking function
Task 1:
Start by changing the call from
Task 2:
Next, add a callback method to the
Task 3:
To finish it up, remove the
``` var fs = require('fs'); fs.readFile('index.html', function(err, contents){ console.log(contents); }); ```
1.4 Running Your Code
While you could go to the website and easily install node, we happen to have a console below where you can practice running node applications.
Go ahead and run that file we just created to read a file off the filesystem with node
1.5 Read File in Server
Now that you know how to create an HTTP server and how to read a file off the filesystem in a non-blocking way, let's try to combine the two.
Instead of just writing a string to the HTTP response, write the contents of
Task 1:
After
Task 2:
Now that you have the file contents, write it to the
Task 3:
To finish up, end the
```js var http = require('http'); var fs = require('fs'); http.createServer(function(request, response) { response.writeHead(200); fs.readFile('index.html', function(err, contents){ response.write(contents); response.end(); }); }).listen(8080); ```
1.6 Issuing a Request
Let's see our new server in action. We've already run ``` $ curl http://localhost:8080 ```
1.7 Writing Response Headers
Up until now all we've been sending into the
Task:
Consult the node documentation, and add a 'Content-Type' of 'text/html' to the response.
```js var http = require('http'); var fs = require('fs'); http.createServer(function(request, response) { response.writeHead(200, { 'Content-Type': 'text/html' }); fs.readFile('index.html', function(err, contents) { response.write(contents); response.end(); }); }).listen(8080); ```
1.8 Response End
Our original Hello server can be shortened since the
Task:
Instead of passing the content to
```js var http = require('http'); http.createServer(function(request, response) { response.writeHead(200); response.end("Hello, this is dog"); }).listen(8080); ```
Here are the names of big companies who uses node js for their apps.
回覆刪除kayseriescortu.com - alacam.org - xescortun.com
回覆刪除MMORPG OYUNLAR
回覆刪除instagram takipçi satın al
tiktok jeton hilesi
TİKTOK JETON HİLESİ
antalya saç ekimi
instagram takipçi satın al
İnstagram takipçi satin al
Metin2 pvp serverler
instagram takipçi satın al
perde modelleri
回覆刪除sms onay
TÜRK TELEKOM MOBİL ÖDEME BOZDURMA
nft nasıl alınır
ANKARA EVDEN EVE NAKLİYAT
trafik sigortası
dedektor
Web Site Kurmak
aşk kitapları