Preamble
Hello friends, I have come back, continuing with the basic Nodejs series, today I will introduce you to Express Router
and Controller. huh. It is true that the other code is still running, but try to think that you are coding everything in the index.js
file and a website needs to manage a lot of things if we throw them all into one file then the management is done. maintain will be very tough. When you subdivide each object to manage, then it is much easier to maintain or develop additional features for the object. Having to edit a file with up to several thousand lines of OMG, it’s already tiring finding and editing in any line: v. Stop rambling, let’s get started
Router
First, I need to create a folder and name it routes
to contain the routers that I define, then I create a js file named product.route.js
to contain the routers of the product. The .router
part is letting me know it’s the router file. If you don’t like it, just leave .js
. Inside the product.route.js
file, we need to require Express
and create a router
object from Express
. Next, we will copy all the routers from the index.js side and replace the app
with the router
. Well forget you must exports
router file so that we will require
it later in the index.js
file. Another note is that in both the index.js
and product.route.js
files, I use the data base connection, so I will separate the part connecting with the databse to a separate file, any file needed will call it. into the. I named it connect.js
with the following content.
1 2 3 4 5 6 7 8 9 10 11 12 | <span class="token keyword">var</span> mysql <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'mysql'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> conn <span class="token operator">=</span> mysql <span class="token punctuation">.</span> <span class="token function">createConnection</span> <span class="token punctuation">(</span> <span class="token punctuation">{</span> host <span class="token operator">:</span> <span class="token string">'localhost'</span> <span class="token punctuation">,</span> user <span class="token operator">:</span> <span class="token string">'root'</span> <span class="token punctuation">,</span> password <span class="token operator">:</span> <span class="token string">'your_password'</span> <span class="token punctuation">,</span> database <span class="token operator">:</span> <span class="token string">'dbTest'</span> <span class="token punctuation">,</span> charset <span class="token operator">:</span> <span class="token string">'utf8_general_ci'</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> module <span class="token punctuation">.</span> exports <span class="token operator">=</span> conn <span class="token punctuation">;</span> |
Next is the product.route.js
file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'express'</span> <span class="token punctuation">)</span> <span class="token keyword">var</span> router <span class="token operator">=</span> <span class="token function">express</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">var</span> conn <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'../connect'</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">''</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token string">'SELECT * FROM posts'</span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/search'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/create'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'create'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">post</span> <span class="token punctuation">(</span> <span class="token string">'/create'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> params <span class="token operator">=</span> req <span class="token punctuation">.</span> body <span class="token punctuation">.</span> title <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">insert into posts(title) values('</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">');</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> result</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"1 record inserted"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/detail'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'detail'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/edit'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'update'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">post</span> <span class="token punctuation">(</span> <span class="token string">'/edit'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> params <span class="token operator">=</span> req <span class="token punctuation">.</span> body <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">update posts set title = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token punctuation">.</span> title <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">' where id = </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token punctuation">.</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">;</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> result</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"1 record update"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/delete'</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">Delete FROM posts where id = </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> module <span class="token punctuation">.</span> exports <span class="token operator">=</span> router <span class="token punctuation">;</span> |
Next, on the index.js
file, I create a productRoute
require object that product.route.js
to
1 2 | <span class="token keyword">var</span> productRoute <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'./routes/product.route'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
and just call it
1 2 | app <span class="token punctuation">.</span> <span class="token function">use</span> <span class="token punctuation">(</span> <span class="token string">''</span> <span class="token punctuation">,</span> productRoute <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
My index.js
file should look like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'express'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">express</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">const</span> port <span class="token operator">=</span> <span class="token number">3000</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> productRoute <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'./routes/product.route'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> conn <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'./connect'</span> <span class="token punctuation">)</span> app <span class="token punctuation">.</span> <span class="token function">set</span> <span class="token punctuation">(</span> <span class="token string">'view engine'</span> <span class="token punctuation">,</span> <span class="token string">'pug'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> app <span class="token punctuation">.</span> <span class="token function">set</span> <span class="token punctuation">(</span> <span class="token string">'views'</span> <span class="token punctuation">,</span> <span class="token string">'./views'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">const</span> bodyParser <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'body-parser'</span> <span class="token punctuation">)</span> app <span class="token punctuation">.</span> <span class="token function">use</span> <span class="token punctuation">(</span> bodyParser <span class="token punctuation">.</span> <span class="token function">urlencoded</span> <span class="token punctuation">(</span> <span class="token punctuation">{</span> extended <span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span> conn <span class="token punctuation">.</span> <span class="token function">connect</span> <span class="token punctuation">(</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">throw</span> err <span class="token punctuation">.</span> stack <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"connect success"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> app <span class="token punctuation">.</span> <span class="token function">use</span> <span class="token punctuation">(</span> <span class="token string">''</span> <span class="token punctuation">,</span> productRoute <span class="token punctuation">)</span> <span class="token punctuation">;</span> app <span class="token punctuation">.</span> <span class="token function">listen</span> <span class="token punctuation">(</span> port <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">error</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> error <span class="token punctuation">)</span> <span class="token punctuation">{</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"Something went wrong"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"server is running port: "</span> <span class="token operator">+</span> port <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> |
Look more compact than before, right: v
Controller
I have already separated the router file, but in fact, the router file is only used to define the router and the processing logic is separated into a separate file called Controller
similar to the MVC model. So I will split the logic on the router side into a new file, the logic here is those functions. Similar to the Router section, we also create a controller
folder and a product.controller.js
file. Now I need to do in the product.controller.js
file, which is exports functions, and the router needs to call it.
For example, the router side has 1 router as follows
1 2 3 4 5 6 7 8 9 | router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">''</span> <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token string">'SELECT * FROM posts'</span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> |
Then on the controller side, I will export to a function named index
as follows:
1 2 3 4 5 6 7 8 9 | module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">index</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token string">'SELECT * FROM posts'</span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Then the router
file side just needs to require
controller
1 2 | <span class="token keyword">var</span> controller <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'../controller/product.controller'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> |
And the router calls controller.index
1 2 | router.get('', controller.index); |
Do the same for other routers and controllers. then I have the controller
file as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | <span class="token keyword">var</span> conn <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'../connect'</span> <span class="token punctuation">)</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">index</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token string">'SELECT * FROM posts'</span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">search</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'index'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">getCreate</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'create'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">postCreate</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> params <span class="token operator">=</span> req <span class="token punctuation">.</span> body <span class="token punctuation">.</span> title <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">insert into posts(title) values('</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">');</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> result</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"1 record inserted"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">detail</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'detail'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">getEdit</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">SELECT * FROM posts where id = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">'</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> res <span class="token punctuation">.</span> <span class="token function">render</span> <span class="token punctuation">(</span> <span class="token string">'update'</span> <span class="token punctuation">,</span> <span class="token punctuation">{</span> posts <span class="token operator">:</span> data <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">postEdit</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> params <span class="token operator">=</span> req <span class="token punctuation">.</span> body <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">update posts set title = '</span> <span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token punctuation">.</span> title <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">' where id = </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> params <span class="token punctuation">.</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token string">;</span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> result</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">if</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> err <span class="token punctuation">)</span> <span class="token punctuation">;</span> console <span class="token punctuation">.</span> <span class="token function">log</span> <span class="token punctuation">(</span> <span class="token string">"1 record update"</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> module <span class="token punctuation">.</span> exports <span class="token punctuation">.</span> <span class="token function-variable function">delete</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">req <span class="token punctuation">,</span> res</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> id <span class="token operator">=</span> <span class="token function">parseInt</span> <span class="token punctuation">(</span> req <span class="token punctuation">.</span> query <span class="token punctuation">.</span> id <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token keyword">var</span> sql <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span> <span class="token string">Delete FROM posts where id = </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span> id <span class="token interpolation-punctuation punctuation">}</span></span> <span class="token template-punctuation string">`</span></span> <span class="token punctuation">;</span> conn <span class="token punctuation">.</span> <span class="token function">query</span> <span class="token punctuation">(</span> sql <span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span> <span class="token parameter">err <span class="token punctuation">,</span> data <span class="token punctuation">,</span> fields</span> <span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token punctuation">}</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> res <span class="token punctuation">.</span> <span class="token function">redirect</span> <span class="token punctuation">(</span> <span class="token string">'/'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> <span class="token punctuation">}</span> |
Well because it uses the databse connection, you remember to require the conn
guy I created above. The router file is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <span class="token keyword">var</span> express <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'express'</span> <span class="token punctuation">)</span> <span class="token keyword">var</span> router <span class="token operator">=</span> <span class="token function">express</span> <span class="token punctuation">(</span> <span class="token punctuation">)</span> <span class="token keyword">var</span> controller <span class="token operator">=</span> <span class="token function">require</span> <span class="token punctuation">(</span> <span class="token string">'../controller/product.controller'</span> <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">''</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> index <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/search'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> search <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/create'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> getCreate <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">post</span> <span class="token punctuation">(</span> <span class="token string">'/create'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> postCreate <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/detail'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> detail <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/edit'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> getEdit <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">post</span> <span class="token punctuation">(</span> <span class="token string">'/edit'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> postEdit <span class="token punctuation">)</span> <span class="token punctuation">;</span> router <span class="token punctuation">.</span> <span class="token function">get</span> <span class="token punctuation">(</span> <span class="token string">'/delete'</span> <span class="token punctuation">,</span> controller <span class="token punctuation">.</span> delete <span class="token punctuation">)</span> <span class="token punctuation">;</span> module <span class="token punctuation">.</span> exports <span class="token operator">=</span> router <span class="token punctuation">;</span> |
Conclude
So I introduced you to the Router and Controller in Express to make our code somewhat clearer and easier to maintain. The article is somewhat flawed, we hope you to comment below for more, if you find the article useful, give me an upvote, Click follow to be able to follow the latest posts. me please. Thank you for your reading.