Nodejs Creating an HTTPS server

Source: Internet
Author: User

Create an express development environment

~ D:\workspace\javascript>express -e  nodejs-https~ D:\workspace\javascript>cd nodejs-https && npm install[email protected] node_modules\ejs[email protected] node_modules\express├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected]├── [email protected] ([email protected])└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected], [email protected])

I have a GIT client installed locally msysgit , which includes the OpenSSL program.

~ d:\workspace\javascript\nodejs-https>git--versiongit Version 1.8.1.msysgit.1~ D:\workspace\javascript\nodejs-https>openssl Version-aopenssl 0.9.8e 2007built on: Sat Sep 20:34:58 EDT 2007platform:msysoptions:bn (64,32) md2 (int) RC4 (idx,int) des (Ptr,risc1,16,long) Blowfish (IDX) CO mpiler:gcc-d_windll-dopenssl_pic-dopenssl_threads-ddso_dlfcn-dhave_dlfcn_h-dtermios-dl_endian-d__cygwin__-fomi T-frame-pointer-fnative-struct-o3-mcpu=pentium-march=i486-wall-dopenssl_bn_asm_part_words-dopenssl_ia32_sse2- Dsha1_asm-dmd5_asm-drmd160_asm-daes_asmopenssldir: "/usr/ssl"  

openssl Generate certificate file

#生成私钥key文件: ~ D:\workspace\javascript\nodejs-https>openssl genrsa-out PRIVATEKEY.PEM 1024Generating RSA private key , modulus...........................++++++........++++++e bit Long is 65537 (0x10001) #通过私钥生成CSR证书签名 ~ D:\workspace \javascript\nodejs-https>openssl Req-new-key privatekey.pem-out Certrequest.csryou is about to being asked to enter in Formation that'll be incorporatedinto your certificate request. What's about-to-enter is called a distinguished Name or a DN. There is quite a few fields but can leave some blankfor some fields there would be a default value,if you enter '. ', t He field would be a left blank.-----Country Name (2 letter code) [Au]:cnstate or province name (full name) [Some-state]:beiji  Nglocality name (eg, city) []:beijingorganization name (eg, company) [Internet widgits Pty ltd]:fens.meorganizational Unit Name (eg, section) []:fens.mecommon name (eg, YOUR name) []:conan zhangemail Address []:[email protected]please ente R the FollowinG ' Extra ' attributesto is sent with your certificate Requesta challenge password []:an optional company name []:# via private key and certificate signing Name Generate certificate file ~ D:\workspace\javascript\nodejs-https>openssl x509-req-in certrequest.csr-signkey privatekey.pem-out Certificate.pemsignature Oksubject=/c=cn/st=beijing/l=beijing/o=fens.me/ou=fens.me/cn=conan Zhang/[email  Protected]getting Private Key

Renaissance 3 Files: Certificate . PEM, CERTREQUEST.CSR, Privatekey.pem

~ d:\workspace\javascript\nodejs-https>ls-ltotal 17- RWX------1 4294967295 mkpasswd 877 Dec 10:53 app.js-rwx------1 4294967295 mkpasswd 956 Dec 11:22 Certificate.pem -RWX------1 4294967295 mkpasswd 704 Dec 11:21 certrequest.csrdrwx------+ 1 4294967295 mkpasswd 0 Dec 11:10 Node_ MODULES-RWX------1 4294967295 mkpasswd 216 Dec 11:03 package.json-rwx------1 4294967295 mkpasswd 887 Dec 11:20 P RIVATEKEY.PEMDRWX------+ 1 4294967295 mkpasswd 0 Dec 10:53 publicdrwx------+ 1 4294967295 mkpasswd 0 Dec 10:53 R OUTESDRWX------+ 1 4294967295 mkpasswd 0 Dec 10:53 views  
    • privatekey.pem: Private key
    • CERTREQUEST.CSR:CSR certificate signature
    • certificate.pem: Certificate file

Modify Startup file: App.js

~ vi app.js//最下面var https = require(‘https‘)    ,fs = require("fs");var options = {    key: fs.readFileSync(‘./privatekey.pem‘),    cert: fs.readFileSync(‘./certificate.pem‘)};https.createServer(options, app).listen(3011, function () {    console.log(‘Https server listening on port ‘ + 3011);});

Start the server:

~ D:\workspace\javascript\nodejs-https>node app.jsExpress server listening on port 3000Https server listening on port 3011

Open Browser: HTTP access

HTTPS access

Nodejs 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.