How to use express to quickly build a nodejs framework for a web project, and the role of project files in express

1. Right-click the folder you want to build, and choose to open in the integrated terminal

2. Download and install express:

命令行语句:npm i express-generator -g

After the express version appears, the installation is successful

2. Create an express project:

Command line statement: express project name

Note: If the creation of the project fails, find the system Windows PowerShell, run it as an administrator, then open the script command line window, enter set-ExecutionPolicy RemoteSignedEnter, if there is Aan option, enter A, if there is no Aoption, enter , press yEnter, close the window, and restart VSCode .

project file explanation

  • bin: store server configuration files.
    • www: Server configuration file, in which the server configuration information is written (no special circumstances, modification is not allowed).
  • public: Public resources that store items.
    • Public resources: resources that can be accessed by users (resources that browsers can directly request from the server).
    • For example: html, css, js, pictures, etc.
  • routes: store routing configuration files.
    • Store the back-end program code.
  • views: store template files.
  • app.js: Project startup entry file. After the file is executed, the server can be started, and after the server is started, the project resource can be accessed by the user (browser request).
  • package.json: Manage configuration files for project packages.

express first experience

  1. Create target project

    express firstExpress
  2. Download the third-party package that the express project depends on (make sure to package.jsonexecute the following command in the upper folder to which the target file belongs)

    1. cd firstExpress

    2. npm i

  3. Create project home page

  4. Configure startup information

    delete app.jsthe last line inmodule.exports = app;
  5. Add the following code in app.jsthe last part of : (check the system port occupancy netstat -ano)
    • app.listen(端口号,服务器启动成功时调用的函数);
    •  
  6. start server

          node app.js

               To close the server, you can choose to close the current terminal or enterctrl+c

        7. The user can access the server resources through the browser

                http://host:port number/resource path

                eg:http://127.0.0.1:3000/html/index.html

     注意:If no resource path is specified, the server will publiclook for files from the folder indexas the requested resource by default;

Guess you like

Origin blog.csdn.net/wzy_PROTEIN/article/details/125428380