@@ -1403,66 +1403,6 @@ Please note - before pid. This converts a pid to a group of pids for process kil
1403
1403
1404
1404
<br />
1405
1405
1406
- ## Q. Can you create http server in Node.js, explain the code used for it?
1407
-
1408
- Yes, we can create HTTP Server in Node.js. We can use the ` <http-server> ` command to do so.
1409
-
1410
- Following is the sample code.
1411
-
1412
- ``` js
1413
- var http = require (' http' );
1414
- var requestListener = function (request , response ) {
1415
- response .writeHead (200 , {' Content-Type' : ' text/plain' });
1416
- response .end (' Welcome Viewers\n ' );
1417
- }
1418
- var server = http .createServer (requestListener);
1419
- server .listen (4200 ); // The port where you want to start with.
1420
- ```
1421
-
1422
- <div align =" right " >
1423
- <b><a href="#table-of-contents">↥ back to top</a></b>
1424
- </div >
1425
-
1426
- ## Q. How to load html in Node.js?
1427
-
1428
- To load HTML in Node.js we have to change the “Content-type” in the HTML code from text/plain to text/html.
1429
-
1430
- ``` js
1431
- fs .readFile (filename, " binary" , function (err , file ) {
1432
- if (err) {
1433
- response .writeHead (500 , {" Content-Type" : " text/plain" });
1434
- response .write (err + " \n " );
1435
- response .end ();
1436
- return ;
1437
- }
1438
-
1439
- response .writeHead (200 );
1440
- response .write (file, " binary" );
1441
- response .end ();
1442
- });
1443
- ```
1444
-
1445
- Now we will modify this code to load an HTML page instead of plain text.
1446
-
1447
- ``` js
1448
- fs .readFile (filename, " binary" , function (err , file ) {
1449
- if (err) {
1450
- response .writeHead (500 , {" Content-Type" : " text/html" });
1451
- response .write (err + " \n " );
1452
- response .end ();
1453
- return ;
1454
- }
1455
-
1456
- response .writeHead (200 , {" Content-Type" : " text/html" });
1457
- response .write (file);
1458
- response .end ();
1459
- });
1460
- ```
1461
-
1462
- <div align =" right " >
1463
- <b><a href="#table-of-contents">↥ back to top</a></b>
1464
- </div >
1465
-
1466
1406
## Q. How can you listen on port 80 with Node?
1467
1407
1468
1408
Instead of running on port 80 we can redirect port 80 to your application\' s port (>1024) using
@@ -1494,15 +1434,18 @@ jwt.sign(payload, secretOrPrivateKey, [options, callback])
1494
1434
* ** Payload** - Contains the claims that provide information about a user who has been authenticated along with other information such as token expiration time.
1495
1435
* ** Signature** - Final part of a token that wraps in the encoded header and payload, along with the algorithm and a secret
1496
1436
1497
- ** Installation**
1437
+ ** Installation: **
1498
1438
1499
1439
``` bash
1500
1440
npm install jsonwebtoken bcryptjs --save
1501
1441
```
1502
1442
1503
- ** Example** : AuthController.js
1443
+ ** Example** :
1504
1444
1505
1445
``` js
1446
+ /**
1447
+ * AuthController.js
1448
+ */
1506
1449
var express = require (' express' );
1507
1450
var router = express .Router ();
1508
1451
var bodyParser = require (' body-parser' );
@@ -1536,10 +1479,12 @@ router.post('/register', function(req, res) {
1536
1479
});
1537
1480
```
1538
1481
1539
- ** config.js**
1482
+ ** config.js: **
1540
1483
1541
1484
``` js
1542
- // config.js
1485
+ /**
1486
+ * config.js
1487
+ */
1543
1488
module .exports = {
1544
1489
' secret' : ' supersecret'
1545
1490
};
0 commit comments