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

  • Node.js 6.x Blueprints
  • Fernando Monteiro
  • 164字
  • 2021-07-14 10:35:04

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.
主站蜘蛛池模板: 藁城市| 吴江市| 泸州市| 奉新县| 金阳县| 丰宁| 苍溪县| 子洲县| 仪征市| 汝阳县| 屏边| 五台县| 连江县| 图木舒克市| 阳原县| 剑河县| 衡山县| 沙坪坝区| 株洲县| 锡林浩特市| 定南县| 纳雍县| 青浦区| 府谷县| 潼关县| 兰州市| 和平区| 朝阳县| 长兴县| 石首市| 蒙城县| 临颍县| 诏安县| 岳西县| 巴林右旗| 承德市| 准格尔旗| 新晃| 鲁山县| 广水市| 营口市|