Skip to content

Commit 1376966

Browse files
committed
Update README.md
1 parent a98999b commit 1376966

File tree

1 file changed

+11
-120
lines changed

1 file changed

+11
-120
lines changed

README.md

Lines changed: 11 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1682,148 +1682,39 @@ const server = app.listen(3000, function () {});
16821682
<b><a href="#table-of-contents">↥ back to top</a></b>
16831683
</div>
16841684

1685-
## Q. Write the steps for setting up an Express JS application?
1686-
1687-
**1. Install Express Generator:**
1688-
1689-
```bash
1690-
C:\node>npm install -g express-generator
1691-
```
1692-
1693-
**2. Create an Express Project:**
1694-
1695-
```bash
1696-
C:\node>express --view="ejs" nodetest1
1697-
```
1698-
1699-
**3. Edit Dependencies:**
1700-
1701-
MAKE SURE TO CD INTO YOUR nodetest FOLDER. OK, now we have some basic structure in there, but we're not quite done. You'll note that the express-generator routine created a file called package.json in your nodetest1 directory. Open this up in a text editor and it\'ll look like this:
1702-
1703-
```js
1704-
// package.json
1705-
{
1706-
"name": "nodetest1",
1707-
"version": "0.0.0",
1708-
"private": true,
1709-
"scripts": {
1710-
"start": "node ./bin/www"
1711-
},
1712-
"dependencies": {
1713-
"cookie-parser": "~1.4.3",
1714-
"debug": "~2.6.9",
1715-
"ejs": "~2.5.7",
1716-
"express": "~4.16.0",
1717-
"http-errors": "~1.6.2",
1718-
"morgan": "~1.9.0"
1719-
}
1720-
}
1721-
```
1722-
1723-
This is a basic JSON file describing our app and its dependencies. We need to add a few things to it. Specifically, calls for MongoDB and Monk.
1724-
1725-
```bash
1726-
C:\node\nodetest1>npm install --save monk@^6.0.6 mongodb@^3.1.13
1727-
```
1728-
1729-
**4. Install Dependencies:**
1730-
1731-
```bash
1732-
C:\node\nodetest1>npm install
1733-
C:\node\nodetest1>npm start
1734-
```
1735-
1736-
Node Console
1737-
1738-
```bash
1739-
> nodetest1@0.0.0 start C:\node\nodetest1
1740-
> node ./bin/www
1741-
```
1742-
1743-
<div align="right">
1744-
<b><a href="#table-of-contents">↥ back to top</a></b>
1745-
</div>
1746-
1747-
## Q. What is your favourite HTTP framework and why?
1748-
1749-
**1. Express.js:**
1750-
1751-
Express provides a thin layer on top of Node.js with web application features such as basic routing, middleware, template engine and static files serving, so the drastic I/O performance of Node.js doesn\'t get compromised.
1752-
1753-
Express is a minimal, un-opinionated framework. it doesn\'t apply any of the prevalent design patterns such as MVC, MVP, MVVM or whatever is trending out of the box. For fans of simplicity, this is a big plus among all other frameworks because you can build your application with your own preference and no unnecessary learning curve. This is especially advantageous when creating a new personal project with no historical burden, but as the project or developing team grows, lack of standardization may lead to extra work for project/code management, and worst case scenario it may lead to the inability to maintain.
1754-
1755-
**2. Generator:**
1756-
1757-
Even though the framework is un-opinionated, it does have the generator that generates specific project folder structure. After installing express-generator npm package and creating application skeleton with generator command, an application folder with clear hierarchy will be created to help you organize images, front-end static JavaScript, stylesheet files and HTML template files.
1758-
1759-
```bash
1760-
npm install express-generator -g
1761-
express helloapp
1762-
```
1763-
1764-
**3. Middleware:**
1765-
1766-
Middleware are basically just functions that have full access to both request and response objects.
1767-
1768-
```js
1769-
const app = express();
1770-
1771-
app.use(cookieParser());
1772-
app.use(bodyParser());
1773-
app.use(logger());
1774-
app.use(authentication());
1775-
1776-
app.get('/', function (req, res) {
1777-
// ...
1778-
});
1779-
1780-
app.listen(3000);
1781-
```
1782-
1783-
An Express application is essentially Node.js with a host of middleware functions, whether you want to customize your own middleware or take advantage of the built-in middlewares of the framework, Express made the process natural and intuitive.
1784-
1785-
**4. Template Engine:**
1786-
1787-
Template engines allow developer to embed backend variables into HTML files, and when requested the template file will be rendered to plain HTML format with the variables interpolated with their actual values. By default, the express-generator uses Pug (originally known as Jade) template engine, but other options like Mustache and EJS also work with Express seamlessly.
1788-
1789-
**5. Database Integration:**
1790-
1791-
As a minimal framework, Express does not consider database integration as a required aspect within its package, thus it leans toward no specific database usage whatsoever. While adopting a particular data storage technology, be it MySQL, MongoDB, PostgreSQL, Redis, ElasticSearch or something else, it\'s just a matter of installing the particular npm package as database driver. These third party database drivers do not conform to unified syntax when doing CRUD instructions, which makes switching databases a big hassle and error prone.
1792-
1793-
<div align="right">
1794-
<b><a href="#table-of-contents">↥ back to top</a></b>
1795-
</div>
1796-
17971685
## Q. Why should you separate Express 'app' and 'server'?
17981686

1799-
Keeping the API declaration separated from the network related configuration (port, protocol, etc) allows testing the API in-process, without performing network calls, with all the benefits that it brings to the table: fast testing execution and getting coverage metrics of the code. It also allows deploying the same API under flexible and different network conditions. Bonus: better separation of concerns and cleaner code.
1687+
Keeping the API declaration separated from the network related configuration (port, protocol, etc) allows testing the API in-process, without performing network calls, with all the benefits that it brings to the table: fast testing execution and getting coverage metrics of the code. It also allows deploying the same API under flexible and different network conditions.
18001688

18011689
API declaration, should reside in app.js:
18021690

18031691
```js
1692+
/**
1693+
* app.js
1694+
*/
18041695
const app = express();
1696+
18051697
app.use(bodyParser.json());
18061698
app.use("/api/events", events.API);
18071699
app.use("/api/forms", forms);
18081700
```
18091701

1810-
Server network declaration, should reside in /bin/www:
1702+
Server network declaration
18111703

18121704
```js
1705+
/**
1706+
* server.js
1707+
*/
18131708
const app = require('../app');
18141709
const http = require('http');
18151710

1816-
/**
1817-
* Get port from environment and store in Express.
1818-
*/
18191711

1712+
// Get port from environment and store in Express.
18201713
const port = normalizePort(process.env.PORT || '3000');
18211714
app.set('port', port);
18221715

1823-
/**
1824-
* Create HTTP server.
1825-
*/
18261716

1717+
// Create HTTP server.
18271718
const server = http.createServer(app);
18281719
```
18291720

0 commit comments

Comments
 (0)