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

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.
主站蜘蛛池模板: 抚顺市| 卢湾区| 内丘县| 蒲城县| 通海县| 建湖县| 威宁| 昂仁县| 分宜县| 盐山县| 宝山区| 洪江市| 忻州市| 罗江县| 泰顺县| 华容县| 周至县| 南丰县| 革吉县| 青龙| 宁化县| 内江市| 兴业县| 鲁甸县| 霸州市| 堆龙德庆县| 北安市| 盱眙县| 陈巴尔虎旗| 自贡市| 慈利县| 威海市| 利辛县| 星子县| 双鸭山市| 呼伦贝尔市| 舟山市| 交城县| 昭苏县| 巴塘县| 河曲县|