書名: Node.js 6.x Blueprints作者名: Fernando Monteiro本章字?jǐn)?shù): 164字更新時(shí)間: 2021-07-14 10:35:04
Creating a controllers folder
- Create a folder called
controllers
inside the root project folder. - Create an
index.js
inside thecontrollers
folder and place the following code:// Index controller exports.show = function(req, res) { // Show index content res.render('index', { title: 'Express' }); };
- Edit the
app.js
file and replace the originalindex
routeapp.use('/', routes);
with the following code:app.get('/', index.show);
- Add the controller path to the
app.js
file right aftervar swig = require('swig');
declaration, replace the original code with the following code:// Inject index controller var index = require('./controllers/index');
- Now it's time to check if all goes as expected: we'll run the application and check the result. Type in your terminal/shell the following command:
npm start
Check the following URL: http://localhost:3000
, and you'll see the welcome message of express framework.
Removing the default routes folder
Let's remove the default routes
folder:
- Remove the
routes
folder and its contents. - Remove the
user route
from theapp.js
, after the index controller line.
推薦閱讀
- 數(shù)據(jù)庫原理及應(yīng)用(Access版)第3版
- Power Up Your PowToon Studio Project
- Selenium Design Patterns and Best Practices
- Data Analysis with IBM SPSS Statistics
- Object-Oriented JavaScript(Second Edition)
- Python Geospatial Development(Second Edition)
- 深入淺出DPDK
- C語言程序設(shè)計(jì)案例式教程
- Mastering KnockoutJS
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Reactive Android Programming
- 程序設(shè)計(jì)基礎(chǔ)教程:C語言
- SQL經(jīng)典實(shí)例(第2版)
- Python之光:Python編程入門與實(shí)戰(zhàn)
- Go語言精進(jìn)之路:從新手到高手的編程思想、方法和技巧(2)