FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

small fixes · waynelinks/practicalnode@278d926 · GitHub

Repository navigation

Commit 278d926

Browse files
committed
small fixes
1 parent 19f89f3 commit 278d926

1 file changed

Lines changed: 28 additions & 27 deletions

File tree

‎chapter2/chapter2.md‎

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ If you write serious apps using only core Node.js modules (refer to the followin
3232
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 &quot;userland&quot;/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]:
3333

3434
```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')
3939

4040
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
4343
mongo.Db.connect(host, (error, client) => {
4444
if (error) throw error;
4545
let collection = new mongo.Collection(
@@ -77,8 +77,8 @@ mongo.Db.connect(host, (error, client) => {
7777
};
7878
}
7979
);
80-
const port = process.env.PORT || 5000;
81-
app.listen(port);
80+
const port = process.env.PORT || 5000
81+
app.listen(port)
8282
})
8383
```
8484

@@ -242,14 +242,16 @@ If you want to install Express.js to an existing project and save the dependency
242242
Create a `server.js` file in the `hello-simple` folder:
243243

244244
```js
245-
const express = require('express');
246-
let app = express();
245+
const express = require('express')
246+
let app = express()
247247

248248
app.all('*', (req, res) => {
249-
res.send('Welcome to the Practical Node.js!');
249+
res.send('Welcome to the Practical Node.js!')
250250
})
251251

252-
app.listen(3000, () => {console.log('Open at localhost:3000')});
252+
app.listen(3000,
253+
() => {console.log('Open at localhost:3000')}
254+
)
253255
```
254256

255257
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
300302

301303
***Figure 2-5.** The result of using Express.js Generator*
302304

303-
If you don&#39;t have computer in front of your right now, here&#39;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&#39;t have computer in front of your right now, here&#39;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)
304306

305307

306308
```js
@@ -600,7 +602,7 @@ This is the second and the last hello world example in this book! :-) The goal i
600602
- Setting up folders
601603
- npm init and package.json
602604
- Dependency declaration
603-
- The app.js file
605+
- The `app.js` file
604606
- Meet Pug
605607
- Running the app
606608
@@ -664,7 +666,7 @@ For the Blog app, we need the following modules, which are the latest as of this
664666
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`:
665667
666668
```js
667-
{
669+
{{
668670
"name": "hello-advanced",
669671
"version": "0.0.1",
670672
"private": true,
@@ -676,7 +678,6 @@ Another way to create a `package.json` file (without using `$ npm init`) is to t
676678
"pug": "2.0.0-rc.4"
677679
}
678680
}
679-
680681
```
681682
682683
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
809810
Here&#39;s the full source code of the `app.js` file for your reference:
810811
811812
```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')
815816

816-
let app = express();
817+
let app = express()
817818

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')
821822

822823
app.all('*', (req, res) => {
823824
res.render(
824825
'index',
825826
{msg: 'Welcome to the Practical Node.js!'}
826-
);
827-
});
827+
)
828+
})
828829

829830
http
830831
.createServer(app)
831832
.listen(
832833
app.get('port'),
833834
() => {
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')}`)
835836
}
836-
);
837+
)
837838
```
838839
839840
Before we can run this server, we need to create the `index.pug` file in the `views` folder.

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL