官术网_书友最值得收藏!

Creating a controllers folder

  1. Create a folder called controllers inside the root project folder.
  2. Create an index.js inside the controllers folder and place the following code:
          // Index controller 
          exports.show = function(req, res) { 
          // Show index content 
              res.render('index', { 
                  title: 'Express' 
              }); 
          }; 
    
  3. Edit the app.js file and replace the original index route app.use('/', routes); with the following code:
          app.get('/', index.show); 
    
  4. Add the controller path to the app.js file right after var swig = require('swig'); declaration, replace the original code with the following code:
          // Inject index controller 
          var index = require('./controllers/index'); 
    
  5. 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:

  1. Remove the routes folder and its contents.
  2. Remove the user route from the app.js, after the index controller line.
主站蜘蛛池模板: 鄂托克前旗| 濉溪县| 西乌珠穆沁旗| 富宁县| 嘉祥县| 绥中县| 龙山县| 西乡县| 奉新县| 宁武县| 万安县| 阆中市| 英吉沙县| 大丰市| 城口县| 玉林市| 安顺市| 凯里市| 民权县| 新蔡县| 阿尔山市| 明溪县| 伊宁县| 资阳市| 利津县| 游戏| 安平县| 临潭县| 凭祥市| 新泰市| 科技| 青海省| 边坝县| 南澳县| 营口市| 琼海市| 潞城市| 陇南市| 望都县| 临澧县| 新乐市|