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

Changing the application's structure

Let's make some changes to the structure of directories in our application and prepare it to follow the Model-View-Controller design pattern.

I will list the necessary steps for this refactoring:

  1. Inside the root project folder:
    • Create a new folder called server
  2. Inside the server folder:
    •  Create a new folder called config
    •  Create a new folder called routes
    • Create a new folder called views.
  3. Do not worry about the config folder at this point; we will insert its contents later.
  4. Now we need to move the error.js and index.js files from the chapter-01/views folder to the chapter-01/server/views folder.
  5. Move the index.js and user.js files from the chapter-01/routes folder to the chapter-01/server/routes folder.
  6. A very simple change here, but during the development process, it will be very useful to better organize all the files of our application.

We still need to change the path to this folder in the main application file, app.js. Open the app.js file from the project root folder and change the following highlighted lines:

... 
var routes = require('./server/routes/index'); var users = require('./server/routes/users');  
var app = express(); 
 
// view engine setup 
app.set('views', path.join(__dirname, 'server/views')); 
app.set('view engine', 'ejs'); 
... 

Before we proceed, let's change the welcome message from the routes/index.js file to the following highlighted code:

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    res.render('index', { title: 'Express from server folder' }); 
}); 

To run the project and see the application in your browser, follow these steps:

  1. Type the following command in your terminal/shell:
    DEBUG=chapter-01:* npm start
    
  2. Open your browser at http://localhost:3000.
  3. The output in your browser will be as follows:

    Application home screen

Now we can delete the folders and files from:

  • chapter-01/routes:
    •  index.js
    •  user.js
  • chapter-01/views:
    •  error.js
    •  index.js

Changing the default behavior to start the application

As mentioned earlier, we will change the default initialization process of our application. To do this task, we will edit the app.js file and add a few lines of code:

  1. Open app.js and add the following code after the app.use('/users', users); function:
          // catch 404 and forward to error handler 
          app.use(function(req, res, next) { 
             var err = new Error('Not Found'); 
              err.status = 404; 
              next(err); 
          }); 
    

    It's a simple middleware to intercept 404 errors.

  2. Now add the following code after the module.exports = app; function:
          app.set('port', process.env.PORT || 3000); 
          var server = app.listen(app.get('port'), function() { 
              console.log('Express server listening on port ' + 
              serer.address().port); 
          }); 
    
  3. Open the package.js file at the root project folder and change the following code:
          ... 
          "scripts": { 
             "start": "node app.js" 
          }, 
          ... 
    

    Note

    The debug command is still available if necessary: DEBUG=chapter-01:* npm start.

  4. The package.json file is a file of extreme importance in Node.js applications. It is possible to store all kinds of information for the project, such as dependencies, project description, authors, version, and many more.
  5. Furthermore, it is possible to set up scripts to minify, concatenate, test, build and deploy an application easily. We'll see more on how to create scripts in Chapter 9, Building a Frontend Process with Node.js and NPM.
  6. Let's test the result; open your terminal/shell and type the following command:
     npm start 
    
  7. We will see the same output on the console:
     > node app.js 
    Express server listening on port 3000!
    
主站蜘蛛池模板: 桐梓县| 南充市| 苗栗县| 拜泉县| 新乡县| 永顺县| 乐平市| 林州市| 溧阳市| 凌海市| 棋牌| 三江| 镇坪县| 祁阳县| 雅安市| 新建县| 曲松县| 大连市| 盐津县| 鹤岗市| 湄潭县| 寿阳县| 汝州市| 宁夏| 巴东县| 图片| 正镶白旗| 德昌县| 南开区| 井陉县| 河源市| 岐山县| 盐池县| 滦平县| 和硕县| 兴业县| 贺州市| 宁德市| 梨树县| 辽宁省| 芮城县|