顯示具有 Nodejs 標籤的文章。 顯示所有文章
顯示具有 Nodejs 標籤的文章。 顯示所有文章

星期二, 8月 13, 2019

[node.js] passport.js OAuth2 api的社群整合

記錄一下撰寫node.js的restful api串接社群按鈕。
基本上流程因前端是react app,
所以會使用前端的fb/google sdk來取得access token,
再提供restful api來驗證access token,
成功後就可以取得Oauth的用戶資訊,
接著就是自已server 用戶資料,如何連結社群的邏輯。

node.js套件

https://github.com/jaredhanson/passport-facebook
https://github.com/jaredhanson/passport-facebook-token
https://github.com/jaredhanson/passport-google-oauth2https://github.com/davidep87/passport-token-google
https://github.com/jmreyes/passport-google-id-token (暫時沒使用)

星期五, 4月 19, 2019

[nodejs] ONLY_FULL_GROUP_BY

MySQL5.7後將sql_mode的ONLY_FULL_GROUP_BY模式預設設定為開啟狀態,這樣一來,很多之前的sql語句可能會出現錯誤,錯誤資訊如下:

查看

select @@global.sql_mode

完整的mysql.cnf覆寫(安裝環境為ubuntu)

sudo vim /etc/mysql/my.cnf (為/etc/mysql/mysql.cnf的軟連結)

[mysqld]
sql_mode = 'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

之後重啟(設定檔如果打錯,mysql會無法開啟,請小心)
sudo service mysql restart

[nodejs] ER_TRUNCATED_WRONG_VALUE_FOR_FIELD 插入資料錯誤

ER_TRUNCATED_WRONG_VALUE_FOR_FIELD

查看mysql character set


SHOW VARIABLES WHERE Variable_name LIKE 'character_set_%' OR Variable_name LIKE 'collation%';


星期一, 4月 01, 2019

[node.js] 偷youtube影音首選 youtube-dl

需要去同步 youtube的資料到server存一份,

有在幹資料的人都會推的開源:youtube-dl
優點是去爬網頁的資料,不需使用 api key也可以使用,
也有支援oauth的功魴。

測試指令


-F 列出所有影片格式


youtube-dl https://www.youtube.com/watch?v=QKm-SOOMC4c -F


-f <格式字串|itag id > -g 列出指定的影片格式 (同事測試六小時會過期)

youtube-dl https://www.youtube.com/watch?v=QKm-SOOMC4c -f best,bestvideo ,
bestaudio -g

星期一, 3月 25, 2019

[ nodejs] invalid-apns-credentials

如發現以下錯誤就是p8的憑證忘記再上傳新開的app裡
誤以為先前測試APP上傳一次就可以一直通用XD

 "error": {
                    "code": "messaging/invalid-apns-credentials",
                    "message": "A message targeted to an iOS device could not be sent because the required APNs SSL certificate was not uploaded or has expired. Check the validity of your development and production certificates."
                }

星期二, 3月 19, 2019

[Nodejs] Use emitter.setMaxListeners() to increase limit 事件監聽超過最大限制

發現nodejs噴出了Use emitter.setMaxListeners() to increase limit 的警告,
找到stackoverflow的討論,但也有人說明是不是要把on 用once取代掉或有on被寫在for回圈裡面。

By default, a maximum of 10 listeners can be registered for any single event.
If it's your code, you can specify maxListeners via:
const emitter = new EventEmitter()
emitter.setMaxListeners(100)
// or 0 to turn off the limit
emitter.setMaxListeners(0)
But if it's not your code you can use the trick to increase the default limit globally:
require('events').EventEmitter.prototype._maxListeners = 100;
Of course you can turn off the limits but be careful:
// turn off limits by default (BE CAREFUL)
require('events').EventEmitter.prototype._maxListeners = 0;
BTW. The code should be at the very beginning of the app.
ADD: Since node 0.11 this code also works to change the default limit:
require('events').EventEmitter.defaultMaxListeners = 0

星期一, 2月 18, 2019

星期二, 1月 29, 2019

[AWS] NODEJS SES (Simple Email Services)寄信

第一次設定ses的流程,有需要的朋友可以試試。
寄送信件有發信跟寄信需要設定。SES的操作介面都有

開始設定

1.進入SES設定
https://console.aws.amazon.com/ses/home

2. 設定domain發信


接續上步之後,就可以看到一堆dns相關資訊,這裡我是設定到cloudflare上。填完設定後就會通過domain認證


有通過認證後,就可以看到verified的綠字了


2. 測試發信

新增一個測試的信件,再去這個信箱發信,點擊驗證link。之後email才能測試寄件


接著你可以用Domain/Email Address來寄信。

Domain寄信
則可以自定要發信的email名稱,這樣就可以依不同服務寄送信件給用戶。EX:註冊信



Email Address寄信
可以用驗證過的信箱寄信給指定信箱


3.設定S3收信

可以寄信就要設定收信了,大多都會將信件存到s3上,
請至Email Receiving做規則設定

4.設定IM

沒有開就不能跟SES API整合,參考錯誤處理2

5.移出沙盒模式

沒有開就不能寄沒驗證過的信箱,參考錯誤處理3



之後就要填寫一些相關的資訊,之後就等support team回覆開通就可以串接了。
查看進度可以點以下連結

nodejs aws-sdk錯誤處理


1. Signature時間逾期
{"message":"Signature expired: 20190129T112038Z is now earlier than 20190129T131757Z

要校正機器的時間設定,因為是裝ubuntu可參考下面的
sudo apt-get install ntp ntpdate
sudo ntpdate ntp.ubuntu.com

2. 權限不足

{"message":"User `arn:aws:iam::161004446739:user/<你的帳號>' is not authorized to perform `ses:SendEmail' on resource `arn:aws:ses:us-east-1:<你的awsid>:identity/tester1@gmail.com'"

要去im設定ses的群組,才能使用

3.信箱沒有驗證

{"message":"Email address is not verified. The following identities failed the check in region US-EAST-1: bigdstut@gmail.com","stack":"MessageRejected: Email address is not verified. 
寄信的對像沒有通過驗證,預設是在沙盒模式中
https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/request-production-access.html?icmpid=docs_ses_console

參考

https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/verify-domain-procedure.html

https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/dns-txt-records.html?icmpid=docs_ses_console

https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/dkim.html

網域dkim
https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/easy-dkim-setup-domain.html

如何設定寫入s3權限
https://docs.aws.amazon.com/zh_tw/ses/latest/DeveloperGuide/receiving-email-permissions.html

寫code
https://github.com/nodemailer/nodemailer-ses-transport
https://medium.com/@yashoda.charith10/sending-emails-using-aws-ses-nodejs-460b8cc6d0d5

http://jsnwork.kiiuo.com/archives/2539/amazon-ses-%E8%A8%AD%E5%AE%9A-email%EF%BC%8C%E8%AE%93%E4%BD%BF%E7%94%A8%E8%80%85%E5%AF%84%E4%BF%A1%E4%BC%BA%E6%9C%8D%E5%99%A8%E6%94%B6%E4%BF%A1%E8%BD%89%E5%AF%84%E7%B5%A6%E7%AE%A1%E7%90%86/

星期二, 1月 15, 2019

星期一, 8月 28, 2017

[node.js] 如何在node.js做好logger的監控

剛開始寫node.js,還是習慣先找一個logger的套件來trace系統。
這篇文章:node-js-logging-tutorial(也完整說明各種log的使用方法)介紹了winston這個套件,有興趣的新手可以試一下。

https://github.com/winstonjs/winston

[node.js] 快快樂樂學node.js (五) 開始連接mysql

現在要來說明如何連結mysql,
這裡使用node.js的mysql套件(https://github.com/mysqljs/mysql#introduction),可以簡單進行與mysql進行連線。

星期三, 8月 16, 2017

[node.js] 快快樂樂學node.js (四) REST API 模組化-使用控制器


在上一篇
[node.js] 快快樂樂學node.js (三) 實作第一個REST API (沒有採用資料庫)

的範例中,並沒有將程式碼考慮到如果有愈多不到資源的REST要處理的話(例如:用戶/Blog等等),
程式碼就會顯示的不好管理,因此我們要將messages拆分到另一個js檔案裡

[node.js] 快快樂樂學node.js (三) 實作第一個REST API (沒有採用資料庫)

由於大家開始習慣所謂的SPA 單一頁面應用程式架構,
前端可以搭任何喜歡的前端框架,例如vue.js/angular/react.js
因此後端就只要負責與前端透過REST API溝通,
REST API的理論就不多說明了,直接記錄在node.js express如何實作

星期二, 8月 15, 2017

[node.js] 快快樂樂學node.js (二) 如何設定自動重啟

先前有說到如果你沒有用任何套件的話,
基本上修改程式過程是很麻煩的,
因此要裝一些套件來達到這件事,
而且一個完整的範例,要包含前後端
早期有一些grunt套件,目前個人則偏愛用gulp相關套件:D

[node.js] 快快樂樂學node.js (一) 啟動第一個express環境

閱讀jollen大大的node.js express的學習筆記,
方便自已速查,有興趣的朋友可以參閱。
github: https://github.com/jollen/nodejs-fullstack-book
我只從express章節開始記錄!!

星期二, 10月 18, 2016

[Node.js] node 指令錯誤

今天在試著建構Vue+Lavavel開發環境,
發現node -v的cli的指令會噴以下錯誤

The program 'node' can be found in the following packages:
 * node
 * nodejs-legacy
Try: sudo apt-get install <selected package>

可以透過以下的指令來解決

sudo ln -s `which nodejs` /usr/local/bin/node

星期四, 6月 12, 2014

[Node.js] node.js 不錯的簡報記錄

node.js已經出來很久了,一直沒有機會來試玩,
就當作今年的學習項目之一啦。 本文記錄 node.js高手分享的簡報。

 

星期三, 6月 04, 2014

[Node.js] Mac 重新安裝npm

今天發現要使用npm install整個噴出異常!!
好像是node的版本太久沒更新了,npm指令整個爛掉。
所以就一直重裝很多次才正常..

系統需求


安裝npm可以透過Mac的套件管理工具homebrew

$ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

安裝完後可以使用doctor來測試brew是否可正常執行

$ brew doctor

Your system is ready to brew.

前情提要 npm


npm is Node's package manager. It is now installed automatically with Node.js so there is no need to do a separate installation.


意思就是npm是一個node.js的套件管理的工具,裝完node.js就會自動安裝好npm了,

不過在Mac下面都是用homebrew這個套件管理工具來安裝node.js

其他你感興趣的文章

Related Posts with Thumbnails