Implementing an HTTPS-based restful style webservice with node. js

Source: Internet
Author: User

1. Create a certificate file

bash-3.2$

bash-3.2$ mkdir SSL

bash-3.2$ CD SSL

bash-3.2$ OpenSSL genrsa-out Key.pem 1024

Generating RSA private key, 1024x768 bit long modulus

....................................++++++

..........++++++

E is 65537 (0x10001)

bash-3.2$ OpenSSL Req-new-key key.pem-out CERTREQUEST.CSR

You is about-to is asked to-enter information that'll be incorporated

into 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 blank

For some fields there would be a default value,

If you enter '. ', the field would be a left blank.

-----

Country Name (2 letter code) [AU]:

State or province name (full name) [Some-state]:

Locality Name (eg, city) []:

Organization Name (eg, company) [Internet widgits Pty LTD]:

Organizational Unit Name (eg, section) []:

Common name (e.g. server FQDN or YOUR name) []:

Email Address []:


Please enter the following ' extra ' attributes

To is sent with your certificate request

A Challenge Password []:

An optional company name []:

bash-3.2$

bash-3.2$

bash-3.2$ OpenSSL x509-req-in certrequest.csr-signkey key.pem-out Cert.pem

Signature OK

Subject=/c=au/st=some-state/o=internet widgits Pty LTD

Getting Private Key

bash-3.2$

bash-3.2$


2. Vim App.js

1 var express = require (' express '), HTTPS = require (' https '), FS = require (' FS ');

2

3 var privatekey = Fs.readfilesync ('./ssl/key.pem '). toString ();

4 var certificate = fs.readfilesync ('./ssl/cert.pem '). toString ();

5 var ca = Fs.readfilesync ('./SSL/CERTREQUEST.CSR '). toString ();

6

7 var options = {

8 Key:privatekey,

9 Cert:certificate,

Ten CA:CA

One }

12

- var app = Express ();

14

//restful API

app.get ("/testapi", function(req, res){

res.send (' Test API ');

);

19

App.get ("/", function(req, res){

res.send (");

);

23

https.createserver (options, App). Listen (443, function() {

- console.log (' HTTPS server started successfully. ' );

(+ });

27

App.listen (80);



3. Node App


4. Open with a browser: Https://127.0.0.1/testapi


This article is from the "GONE with the Wind" blog, please be sure to keep this source http://h2appy.blog.51cto.com/609721/1701038

Implementing an HTTPS-based restful style webservice with node. js

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.