You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -1682,148 +1682,39 @@ const server = app.listen(3000, function () {});
1682
1682
<b><a href="#table-of-contents">↥ back to top</a></b>
1683
1683
</div>
1684
1684
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.
<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
-
constapp=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
-
<divalign="right">
1794
-
<b><a href="#table-of-contents">↥ back to top</a></b>
1795
-
</div>
1796
-
1797
1685
## Q. Why should you separate Express 'app' and 'server'?
1798
1686
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.
1800
1688
1801
1689
API declaration, should reside in app.js:
1802
1690
1803
1691
```js
1692
+
/**
1693
+
* app.js
1694
+
*/
1804
1695
constapp=express();
1696
+
1805
1697
app.use(bodyParser.json());
1806
1698
app.use("/api/events", events.API);
1807
1699
app.use("/api/forms", forms);
1808
1700
```
1809
1701
1810
-
Server network declaration, should reside in /bin/www:
1702
+
Server network declaration
1811
1703
1812
1704
```js
1705
+
/**
1706
+
* server.js
1707
+
*/
1813
1708
constapp=require('../app');
1814
1709
consthttp=require('http');
1815
1710
1816
-
/**
1817
-
* Get port from environment and store in Express.
1818
-
*/
1819
1711
1712
+
// Get port from environment and store in Express.
0 commit comments