Nodejs+express Creating an HTTPS server

Source: Internet
Author: User

Tag: TPS status exists service var request private key Protocol HTTPS

To enable my Nodejs server to provide HTTPS services, I learned how to use Express to create an HTTPS server, which is now logged as follows. (1.1 Points of accumulation and mastery of it)

1. HTTP and HTTPS

Introduced

  • HTTP: Hypertext Transfer Protocol (Hypertext Transfer Protocol) is a rule that specifies the communication between the browser and the World Wide Web server, transmitting the data transfer Protocol of the World Wide Web document over the Internet.
  • HTTPS: (Hypertext Transfer Protocol over secure Socket Layer) is a security-targeted HTTP channel and is simply a secure version of HTTP. That is, the SSL layer is added under HTTP, the security base of HTTPS is SSL, so the detailed content of encryption needs SSL. It is a URI scheme (abstract identifier system) with syntax similar to http: System. For secure HTTP data transfer. Https:url indicates that it uses HTTP, but HTTPS has a different default port than HTTP and an encryption/authentication layer (between HTTP and TCP). The initial development of the system, conducted by Netscape, provides an authentication and encryption method of communication, which is now widely used in security-sensitive communications on the World Wide Web, such as transaction payments.

Difference

  • The HTTPS protocol requires a certificate to be applied to the CA, and the general free certificate is very small and requires a fee.
  • HTTP is a Hypertext Transfer Protocol, the information is plaintext transmission, HTTPS is a secure SSL encryption transport protocol.
  • HTTP and HTTPS use a completely different connection, the same port, the former is 80, the latter is 443.
  • The HTTP connection is simple and stateless; The HTTPS protocol is a network protocol built by the SSL+HTTP protocol for encrypted transmission and authentication, which is more secure than the HTTP protocol.
2. Create an HTTPS server using Express

In Nodejs, we can implement HTTPS servers through the built-in HTTPS libraries.

    • First, we need to generate the certificate file using OpenSSL:
1024 > /path/to/private.pem//#通过私钥文件生成CSR证书签名openssl req -new -key /path/to/private.pem -out csr.pem//#通过私钥文件和CSR证书签名生成证书文件openssl x509 -req -days 365 -in csr.pem -signkey /path/to/private.pem -out /path/to/file.crt

The freshmen became three files:

  • PRIVATE.PEM: Private key
  • Csr.pem:CSR Certificate Signing
  • FILE.CRT: Certificate file
    • Modify the Nodejs startup file Server.js:
var app =Require' Express ') ();var fs =Require' FS ');var http =Require' http ');var https =Require' https ');var Privatekey = Fs.readfilesync ('/path/to/private.pem ',' UTF8 '),var certificate = Fs.readfilesync ('/path/to/file.crt ',' UTF8 ');var credentials = {Key:privatekey,Cert:certificate};var httpserver = http.createserver (APP);var httpsserver = Https.createserver (credentials, app);var PORT =18080;var sslport =18081;httpserver.listen (PORT,function) {console.log ( ' HTTP Server is running on:http://localhost:%s ', (PORT);}); Httpsserver.listen (Sslport, function ( console.log ( ' HTTPS Server is Running on:https://localhost:%s ', Sslport);}); //welcomeapp.get ( '/', function (req, res) {if (req.protocol = = =  ' https ') {Res.status (200). Send ( ' Welcome to Safety land! ');} else {res.status (200). Send ( ' Welcome! ');});   
    • Start the server:
      is running on: http://localhost:18080HTTPS Server is running on: https://localhost:18081
    • Open your browser

HTTP access:


HTTP access

HTTPS access:


HTTPS access

To view a certificate:


View certificates


Wosign Authentication Certificate


At this point, we have successfully created an HTTPS server with Nodejs built-in HTTPS and express.

Nodejs+express Creating an HTTPS server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.