在雲服務器上部署node.js服務器

本文檔介紹騰訊雲·萬象優圖服務端nodejs的部署和集成,搭建一個nodejs+nginx爲基礎,對web端或者移動端提供http簽名接口服務的例子程序。
注意:本文檔只是簡單的示例,展現了服務端爲終端提供簽名的基本示例,開發者務必根據自身業務開發相應的鑑權服務邏輯,並集成到自身服務器中。node

1 環境準備

下面以在騰訊云云服務器CentOS 6.2 64位上安裝nginx爲例,簡單介紹如何將騰訊雲萬象優圖集成,對web端或者移動端提供http簽名接口服務所須要的基礎環境搭建。開發者能夠根據本身業務的須要,構建http或者非http服務,爲自身業務的web端、移動端提供簽名。nginx

1.1 安裝nginx

yum install nginx –y service nginx restart 

1.2 驗證nginx

直接訪問雲服務器ip地址,驗證nginx是否已經運行起來。web

2 安裝配置Nodejs環境

下面介紹安裝Nodejs和配置web container的詳細步驟。
1 安裝Nodejsnpm

yum install -y nodejs npm 

2 配置web container
修改/etc/nginx/conf.d/default.conf以下:json

# # The default server # server { listen 80 default_server; server_name _; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location ^~ /node/ { proxy_pass http://localhost:9002; } } 

3 從新加載nginx配置
修改配置完成後,須要執行如下命令從新加載配置。服務器

nginx -s reload 

3 安裝Nodejs SDK

執行如下命令安裝Nodejs SDK。app

cd /data/www/tencentyun/node npm install tencentyun 

4 開發鑑權服務邏輯

將sdk集成到開發者代碼,開發鑑權服務邏輯,這裏以node目錄下getsignv2.js爲例(開發者務必根據自身業務開發相應的鑑權服務邏輯):
注意:若是開發者想按照本示例作簡單地測試,須要將下面代碼中的相應字段替換爲本身的項目信息,具體見代碼註釋。測試

var http=require('http'); var url = require('url'); var util = require('util'); var tencentyun = require('tencentyun'); var server=new http.Server(); server.on('request',function(req,res){ var urlinfo = url.parse(req.url,true), type = 'upload'; if (urlinfo.query && urlinfo.query.type) { type = urlinfo.query.type; } //請將下面的bucket, projectId, secretId和secretKey替換成開發者本身的項目信息 var bucket = 'test0706', projectId = '10000037', userid = 0, secretId = 'AKIDpoKBfMK7aYcYNlqxnEtYA1ajAqji2P7T', secretKey = 'P4FewbltIpGeAbwgdrG6eghMUVlpmjIe'; tencentyun.conf.setAppInfo(projectId, secretId, secretKey); var error = false; switch(type) { case 'upload': var fileid = '/u/can/use/slash/sample' + Math.round(+new Date()/1000), expired = Math.round(+new Date()/1000) + 999, uploadurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid), sign = tencentyun.auth.getAppSignV2(bucket, fileid, expired); ret = {'sign':sign,'url':uploadurl}; break; case 'stat': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), otherurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid), ret = {'url':otherurl}; } break; case 'del': case 'copy': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), otherurl = tencentyun.imagev2.generateResUrlV2(bucket, userid, fileid, type), sign = tencentyun.auth.getAppSignV2(bucket, fileid, 0); ret = {'sign':sign,'url':otherurl}; } break; case 'download': if (!urlinfo.query || !urlinfo.query.fileid) { error = true; } else { var fileid = decodeURIComponent(urlinfo.query.fileid), expired = Math.round(+new Date()/1000) + 999, sign = tencentyun.auth.getAppSignV2(bucket, fileid, expired); ret = {'sign':sign}; } break; } res.writeHead(200,{'Content-Type':'application/json'}); if (error) { res.end({'error':'params error'}); } else { res.end(JSON.stringify(ret)); } }); server.listen(9002); console.log('HTTP SERVER is LISTENING AT PORT 9002.'); 

5 運行程序

cd /data/www/tencentyun/node nohup node getsignv2.js & 

6 測試

  1. 終端經過CGI:http://203.195.194.28/node/?type=[opType]&fileid=[fileid]來獲取相應的簽名。
    opType:可取值:upload(上傳), stat(查詢), copy(複製), del(刪除)和download(下載,若是開啓token防盜鏈);
    fileid:是圖片資源的惟一標識;當opType爲upload時,若是開發者沒有指定fileid,fileid置空,不然指定爲相應的fileid;下載簽名,fileid能夠空,也能夠爲開發者查看的圖片fileid。
    注意: 下載簽名只有開發者在控制檯上面設置了token防盜鏈時才使用,若是沒有token防盜鏈,不須要下載簽名,直接使用下載url下載圖片。
    示例:
    http://203.195.194.28/node/?type=del&fileid=sample123 http://203.195.194.28/node/?type=copy&fileid=sample123 http://203.195.194.28/node/?type=stat&fileid=sample123 http://203.195.194.28/node/?type=download&fileid=sample123 http://203.195.194.28/node/?type=upload&fileid=sample123
相關文章
相關標籤/搜索