# 2.8.Create free ssl for express web server

* 1.本篇記錄使用ssl for free (<https://www.sslforfree.com/)申請一個免費的ssl> certificates, 並將它加到node express web server中
* 2.首先必須先要有一個自己的domain name, 並將此domain name輸入到欄位中

![](/files/-M4M0Nl3tKBna7n76ZK9)

* 3.按下按鈕後, 選擇**Manual Verification**

![](/files/-M4M0Nl5Kn9KWBr2zCvW)

* 4.按下**Download File #1**下載verification file

![](/files/-M4M0Nl7x2yfRr6FTC9_)

* 5.在專案根目錄下增加以下路徑:.well-known/acme-challenge
* 6.修改路由(routes/index.js),增加以下程式碼

  ```
    app.get('/.well-known/acme-challenge/<verification file name>', function(req, res, next) 
    {
          res.sendfile('.well-known/acme-challenge/<verification file name>'); 
     });
  ```
* 7.將web server的listen port改為80 port, 啟動時輸入sudo node app.js

  ```
                    app.listen(80);
  ```
* 8.啟動web server, 並按下第5點的連結, 確定web server可下載到verification file, 接著按下**Download SSL Certificate**
* 9.可以下載到一些檔案, 將**certificate.crt**及**private.key**放到專案根目錄下, 並修改app.js

```
                    var privateKey = fs.readFileSync('./private.key', 'utf8');
                    var certificate = fs.readFileSync('./certificate.crt', 'utf8');
                    var credentials = {
                      key: privateKey,
                      cert: certificate
                    };
                    var httpsServer = https.createServer(credentials, app);
                    httpsServer.listen(3000);
```

* 10.重新啟動server即可


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jen-hsuan-hsieh.gitbook.io/javascript-node-js/chapter2-nodejs/28create-free-ssl-for-express-web-server.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
