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

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.
主站蜘蛛池模板: 苍南县| 大方县| 赞皇县| 华蓥市| 大埔区| 项城市| 荣昌县| 社会| 海兴县| 蓬安县| 衡南县| 于都县| 盘山县| 新民市| 都匀市| 秭归县| 睢宁县| 温泉县| 桂林市| 射阳县| 扶沟县| 林周县| 遂溪县| 香河县| 平凉市| 宜春市| 仁怀市| 永年县| 莲花县| 芦山县| 昌都县| 八宿县| 都兰县| 龙井市| 万盛区| 大化| 新干县| 民县| 九龙县| 桓台县| 麻城市|