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 RemoteSigned
Enter, if there is A
an option, enter A
, if there is no A
option, enter , press y
Enter, 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
-
Create target project
express firstExpress
-
Download the third-party package that the express project depends on (make sure to
package.json
execute the following command in the upper folder to which the target file belongs)-
cd
firstExpress
-
npm i
-
-
Create project home page
-
Configure startup information
deleteapp.js
the last line inmodule.exports = app;
- Add the following code in
app.js
the last part of : (check the system port occupancynetstat -ano
)app.listen(端口号,服务器启动成功时调用的函数);
-
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 public
look for files from the folder index
as the requested resource by default;