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

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.
主站蜘蛛池模板: 广水市| 固镇县| 高青县| 岢岚县| 肇东市| 武功县| 隆尧县| 邢台市| 黔南| 五峰| 兰西县| 南木林县| 从化市| 珲春市| 基隆市| 朝阳区| 兰西县| 舞钢市| 丹阳市| 金湖县| 汉寿县| 涪陵区| 新田县| 杂多县| 西盟| 广饶县| 鞍山市| 孙吴县| 南木林县| 韶山市| 桦南县| 富平县| 辉南县| 桃园市| 隆子县| 通化县| 容城县| 南涧| 河东区| 鄄城县| 浮山县|