- Node.js 6.x Blueprints
- Fernando Monteiro
- 164字
- 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.
推薦閱讀
- 數據庫系統原理及MySQL應用教程(第2版)
- Visual Basic .NET程序設計(第3版)
- JavaScript+jQuery開發實戰
- 基于Struts、Hibernate、Spring架構的Web應用開發
- 深入淺出React和Redux
- Android玩家必備
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Java圖像處理:基于OpenCV與JVM
- MATLAB GUI純代碼編寫從入門到實戰
- Python青少年趣味編程
- 計算語言學導論
- Yii2 By Example
- Android開發權威指南(第二版)
- IBM DB2 9.7 Advanced Application Developer Cookbook
- Java EE框架開發技術與案例教程