current position:Home>node. Js-3 step out the use of a server and express package

node. Js-3 step out the use of a server and express package

2022-04-29 16:26:56Suggest running


use http Module write a simple web The server

First step : Create a new file , be known as  d:/src/http.js( The file name and path name can be set by yourself , Chinese characters are not recommended ), The contents are as follows

// 1.  introduce http modular 
const http = require('http');

// 2.  Create services 
const server = http.createServer(function(req, res) {
// After entering the request header end Before you can return to Chinese , Otherwise, there will be confusion 
res.setHeader('content-type', 'text/html;charset=utf8')
  //  Send content to clients , And end this response 
  res.end('I wanted to see how far I could go');
// 3.  Start the service 
server.listen(8081, function() {
  console.log(' Server started successfully , Please be there. http://localhost:8081 Medium visit ....');
 Copy code 

The second step : function js Code , Start the service .

Enter into... Through a small black window 01http.js directory , Enter command node 01http.js, A small black window will pop up , Don't turn off .

The third step : Testing capabilities

Open a browser page , Enter the address :http://localhost:8081, Observe the effect :

  • Effect in browser
  • Effect in small black window

Out of Service :ctrl + c

Be careful :

  • Don't close the small black window , It's the server

  • The server itself will not take the initiative ( The little black window doesn't look any different ), It is always waiting for the client to access

  • Don't use the mouse to select the contents of the small black window , Will cause the program to fake death

  • After modifying the code, restart

    • change res.end() The content of , restart after , Observe again .
    • Restart the service : Is to run the program again ( Press the up arrow , Enter again ).

    The code is represented by a diagram


Several common file types and content-type

  • .html:res.setHeader('content-type', 'text/html;charset=utf8')
  • .css:res.setHeader('content-type', 'text/css;charset=utf8')
  • .js:res.setHeader('content-type', 'application/javascript')
  • .png:res.setHeader('content-type', 'image/png')
  • json data :res.setHeader('content-type', 'application/json;charset=utf-8')

Other types , Reference here…

Express Basic introduction

Express It's based on Node.js platform , Fast 、 to open up 、 minimalist web Development framework .

  • frame : It's a semi-finished product , Used to quickly solve a class of problems ; Libraries are toolsets , Very flexible to use ) ( Framework has :bootstrap, lay-ui, express, vue, react ; library :zepto.js , jQuery,  day.js, underscore, lodash, art-template, axios, echart.....)
  • web Development : Be able to display pages for different requests ; Providing interface services ;

Reference link

understand :

  • We use... In front of us http Module to support web service , Now use express To write web service
  • about node.js Come on ,Express It's a third-party module , Have a lot of API Support , Powerful and flexible middleware characteristic

Express Use

  • Create a new folder , Suppose the name is new-express( The directory name should not have Chinese characters , And don't want the name of a bag ), Run in this directory npm init -y Command to generate package.json file

  • Consistent with the installation of other third-party packages , Local installation npm i express

  • Create a new one under the project root js file , for example app.js, The input code is as follows :

// 0.  load  Express
const express = require('express')

// 1.  call  express()  Get one  app
const app = express()

// Static resource delegation 

// 3.  Monitor port number , start-up  Web  service 
app.listen(3000, () => console.log('app listening on port 3000!'))
 Copy code 

public It's a folder I created myself , what html,js,css,img You can throw it all in . When running, the required resources will automatically go to public Rummage through the folder . Please check the document for specific use

copyright notice
author[Suggest running],Please bring the original link to reprint, thank you.

Random recommended