Express introduces framework and simple to use

express Introduction

  1. The existence of the framework is to help our development efficiency tips exist

  2. Based on Node.js platform, fast, open, minimalist Web development framework

  3. In order to use static files such as images, CSS files, JavaScript files, and the like, using the Express in express.static built middleware functions.

    node is no paper folder concept (container), the introduction of the resources we need to make some judgment can be achieved
    if more content, then load it too much trouble to
    express to help us solve this problem
    express only need one line of code you can specify the contents of the folder inside as your static resources

Preparation before use

Download the file folder frame express your location
npm install express -S

Simple to use

Pure code:

const express = require("express");
const app = express();
app.use(express.static(__dirname));
app.listen(80,function(){
	console.log("success");
})

Code Description:

const express = require("express");   //导入express服务
const app = express();  //得到 express 函数给你返回的值,把它赋值给 app
			
app.use(express.static(__dirname));  //app.use 这个 use 就是应用,你要使用什么东西
									 //express 当中给你提供了一个插件 static ,指定你的某一个文件夹作为静态资源

app.listen(80,function(){     //第一个设置一个端口号,第二个参数是一个函数,这个函数是当我的服务器执行完成以后执行的
	console.log("success");

})

run

After the code is entered to complete the operation to change the use of node js file
enter 127.0.0.1 in a browser
by default it will be your main page as index.html

Published 63 original articles · won praise 6 · views 1233

Guess you like

Origin blog.csdn.net/qq_44163269/article/details/105006535