| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,14 +32,14 @@ If you write serious apps using only core Node.js modules (refer to the followin | |||
| 32 | 32 | To illustrate my point, here is an example of a two-route [representational state transfer](http://en.wikipedia.org/wiki/Representational_state_transfer) (REST) API server, i.e., we have only two endpoints and they are also called *routes*. In this application, we use only core Node.js modules for server functions. A single "userland"/external native MongoDB driver module is used for persistence. This example is taken from beginner-friendly [Full Stack JavaScript](https://github.com/azat-co/fullstack-javascript) (<http://https://github.com/azat-co/fullstack-javascript>)[Apress, 2018]: | |
| 33 | 33 | ||
| 34 | 34 | ```js | |
| 35 | - const http = require('http'); | ||
| 36 | - const util = require('util'); | ||
| 37 | - const querystring = require('querystring'); | ||
| 38 | - const mongo = require('mongodb'); | ||
| 35 | + const http = require('http') | ||
| 36 | + const util = require('util') | ||
| 37 | + const querystring = require('querystring') | ||
| 38 | + const mongo = require('mongodb') | ||
| 39 | 39 | ||
| 40 | 40 | const host = process.env.MONGOHQ_URL || | |
| 41 | - 'mongodb://@127.0.0.1:27017'; | ||
| 42 | - //MONGOHQ_URL=mongodb://user:pass@server.mongohq.com/db_name | ||
| 41 | + 'mongodb://@127.0.0.1:27017' | ||
| 42 | + // MONGOHQ_URL=mongodb://user:pass@server.mongohq.com/db_name | ||
| 43 | 43 | mongo.Db.connect(host, (error, client) => { | |
| 44 | 44 | if (error) throw error; | |
| 45 | 45 | let collection = new mongo.Collection( | |
@@ -77,8 +77,8 @@ mongo.Db.connect(host, (error, client) => { | |||
| 77 | 77 | }; | |
| 78 | 78 | } | |
| 79 | 79 | ); | |
| 80 | - const port = process.env.PORT || 5000; | ||
| 81 | - app.listen(port); | ||
| 80 | + const port = process.env.PORT || 5000 | ||
| 81 | + app.listen(port) | ||
| 82 | 82 | }) | |
| 83 | 83 | ``` | |
| 84 | 84 | ||
@@ -242,14 +242,16 @@ If you want to install Express.js to an existing project and save the dependency | |||
| 242 | 242 | Create a `server.js` file in the `hello-simple` folder: | |
| 243 | 243 | ||
| 244 | 244 | ```js | |
| 245 | - const express = require('express'); | ||
| 246 | - let app = express(); | ||
| 245 | + const express = require('express') | ||
| 246 | + let app = express() | ||
| 247 | 247 | ||
| 248 | 248 | app.all('*', (req, res) => { | |
| 249 | - res.send('Welcome to the Practical Node.js!'); | ||
| 249 | + res.send('Welcome to the Practical Node.js!') | ||
| 250 | 250 | }) | |
| 251 | 251 | ||
| 252 | - app.listen(3000, () => {console.log('Open at localhost:3000')}); | ||
| 252 | + app.listen(3000, | ||
| 253 | + () => {console.log('Open at localhost:3000')} | ||
| 254 | + ) | ||
| 253 | 255 | ``` | |
| 254 | 256 | ||
| 255 | 257 | Then launch it with `node server.js` to see "Welcome to the Practical Node.js!" in a browser at <http://localhost:3000/>. You first Express app is working! | |
@@ -300,7 +302,7 @@ Open the browser of your choice at <http://localhost:3000> and you'll see "Expre | |||
| 300 | 302 | ||
| 301 | 303 | ***Figure 2-5.** The result of using Express.js Generator* | |
| 302 | 304 | ||
| 303 | - If you don't have computer in front of your right now, here's the full code of `express-styl/app.js` using Express.js Generator v4.15.0. The server file has routes from `routes` folder, stylus and a rudimentary error handler. | ||
| 305 | + If you don't have computer in front of your right now, here's the full code of `express-styl/app.js` using Express.js Generator v4.15.0. The server file has routes from `routes` folder, stylus and a rudimentary error handler (the `;` and `var` style is preserved from the code generated by the tool) | ||
| 304 | 306 | ||
| 305 | 307 | ||
| 306 | 308 | ```js | |
@@ -600,7 +602,7 @@ This is the second and the last hello world example in this book! :-) The goal i | |||
| 600 | 602 | - Setting up folders | |
| 601 | 603 | - npm init and package.json | |
| 602 | 604 | - Dependency declaration | |
| 603 | - - The app.js file | ||
| 605 | + - The `app.js` file | ||
| 604 | 606 | - Meet Pug | |
| 605 | 607 | - Running the app | |
| 606 | 608 | ||
@@ -664,7 +666,7 @@ For the Blog app, we need the following modules, which are the latest as of this | |||
| 664 | 666 | Another way to create a `package.json` file (without using `$ npm init`) is to type or copy and paste `package.json` and run `$ npm install`: | |
| 665 | 667 | ||
| 666 | 668 | ```js | |
| 667 | - { | ||
| 669 | + {{ | ||
| 668 | 670 | "name": "hello-advanced", | |
| 669 | 671 | "version": "0.0.1", | |
| 670 | 672 | "private": true, | |
@@ -676,7 +678,6 @@ Another way to create a `package.json` file (without using `$ npm init`) is to t | |||
| 676 | 678 | "pug": "2.0.0-rc.4" | |
| 677 | 679 | } | |
| 678 | 680 | } | |
| 679 | - | ||
| 680 | 681 | ``` | |
| 681 | 682 | ||
| 682 | 683 | In the end, the `node_modules` folder should be filled with the corresponding libraries. | |
@@ -809,31 +810,31 @@ You can also use `https.createServer(app).listen()` for the HTTPS support when y | |||
| 809 | 810 | Here's the full source code of the `app.js` file for your reference: | |
| 810 | 811 | ||
| 811 | 812 | ```js | |
| 812 | - const express = require('express'); | ||
| 813 | - const http = require('http'); | ||
| 814 | - const path = require('path'); | ||
| 813 | + const express = require('express') | ||
| 814 | + const http = require('http') | ||
| 815 | + const path = require('path') | ||
| 815 | 816 | ||
| 816 | - let app = express(); | ||
| 817 | + let app = express() | ||
| 817 | 818 | ||
| 818 | - app.set('port', process.env.PORT || 3000); | ||
| 819 | - app.set('views', path.join(__dirname, 'views')); | ||
| 820 | - app.set('view engine', 'pug'); | ||
| 819 | + app.set('port', process.env.PORT || 3000) | ||
| 820 | + app.set('views', path.join(__dirname, 'views')) | ||
| 821 | + app.set('view engine', 'pug') | ||
| 821 | 822 | ||
| 822 | 823 | app.all('*', (req, res) => { | |
| 823 | 824 | res.render( | |
| 824 | 825 | 'index', | |
| 825 | 826 | {msg: 'Welcome to the Practical Node.js!'} | |
| 826 | - ); | ||
| 827 | - }); | ||
| 827 | + ) | ||
| 828 | + }) | ||
| 828 | 829 | ||
| 829 | 830 | http | |
| 830 | 831 | .createServer(app) | |
| 831 | 832 | .listen( | |
| 832 | 833 | app.get('port'), | |
| 833 | 834 | () => { | |
| 834 | - console.log(`Express.js server listening on port ${app.get('port')}`); | ||
| 835 | + console.log(`Express.js server is listening on port ${app.get('port')}`) | ||
| 835 | 836 | } | |
| 836 | - ); | ||
| 837 | + ) | ||
| 837 | 838 | ``` | |
| 838 | 839 | ||
| 839 | 840 | Before we can run this server, we need to create the `index.pug` file in the `views` folder. | |
| Back | FazBrowse Home | New Git URL |
0 commit comments