{"id":1615,"date":"2014-12-16T19:32:04","date_gmt":"2014-12-16T19:32:03","guid":{"rendered":"https:\/\/www.couchbase.com\/blog\/?p=1615"},"modified":"2023-06-28T00:23:52","modified_gmt":"2023-06-28T07:23:52","slug":"game-servers-and-couchbase-nodejs-part-2","status":"publish","type":"post","link":"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-2\/","title":{"rendered":"Servidores de jogos e Couchbase com Node.js - Parte 2"},"content":{"rendered":"<p>Nesta parte da s\u00e9rie, implementaremos o gerenciamento de sess\u00f5es e os pontos de extremidade autenticados (pontos de extremidade que exigem que voc\u00ea esteja conectado). Vamos come\u00e7ar!<\/p>\n<p>Se voc\u00ea ainda n\u00e3o leu <a href=\"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-1\/\">Parte 1<\/a> desta s\u00e9rie, sugiro que o fa\u00e7a, pois ele define o layout b\u00e1sico do projeto, bem como o gerenciamento b\u00e1sico de usu\u00e1rios, e \u00e9 um pr\u00e9-requisito para a Parte 2!<\/p>\n<h1>Gerenciamento de sess\u00f5es - O modelo<\/h1>\n<p>A primeira etapa para a cria\u00e7\u00e3o de nossos pontos de extremidade de gerenciamento de sess\u00e3o \u00e9 a configura\u00e7\u00e3o de um modelo a ser usado a partir desses pontos de extremidade para manipular o banco de dados. Ao contr\u00e1rio do modelo de conta, esse modelo n\u00e3o precisa de documentos referenciais nem de nenhuma l\u00f3gica complicada e, portanto, \u00e9 bastante simples. A primeira etapa \u00e9 importar os v\u00e1rios m\u00f3dulos de que precisaremos, bem como obter uma refer\u00eancia \u00e0 nossa conex\u00e3o de banco de dados do nosso m\u00f3dulo de banco de dados.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">var<\/span> db <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'.\/..\/database'<\/span><span style=\"color: #009900\">)<\/span>.<span style=\"color: #660066\">mainBucket<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> couchbase <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'couchbase'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> uuid <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'uuid'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>Em seguida, mais uma vez de forma semelhante ao nosso modelo de conta, criamos uma pequena fun\u00e7\u00e3o para remover as propriedades do nosso banco de dados. 'type' \u00e9 a \u00fanica que precisamos remover nesse caso tamb\u00e9m.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span> cleanSessionObj<span style=\"color: #009900\">(<\/span>obj<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">excluir<\/span> obj.<span style=\"color: #660066\">tipo<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> obj<span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/div>\n<\/div>\n<p>Agora vamos come\u00e7ar a trabalhar na pr\u00f3pria classe do modelo de sess\u00e3o. Primeiro vem nosso construtor em branco. Gostaria de mencionar neste momento que nossas classes de modelo s\u00e3o totalmente est\u00e1ticas neste exemplo, mas uma boa pr\u00e1tica a ser seguida \u00e9 retornar suas classes de modelo das opera\u00e7\u00f5es CRUD est\u00e1ticas, mas ainda n\u00e3o chegamos ao ponto em que isso seria \u00fatil.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span> SessionModel<span style=\"color: #009900\">(<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><br \/>\nm\u00f3dulo.<span style=\"color: #660066\">exporta\u00e7\u00f5es<\/span> <span style=\"color: #339933\">=<\/span> SessionModel<span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>Agora, para nossa primeira fun\u00e7\u00e3o de modelo de sess\u00e3o que realmente far\u00e1 algum trabalho.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">SessionModel.<span style=\"color: #660066\">criar<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>uid<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>Dentro dessa fun\u00e7\u00e3o, precisamos criar nosso documento que ser\u00e1 inserido e sua chave associada (armazenamos apenas o uid e, em seguida, acessamos as informa\u00e7\u00f5es restantes dos usu\u00e1rios diretamente do documento da conta por meio do nosso modelo de conta, que voc\u00ea ver\u00e1 mais adiante.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">var<\/span> sessDoc <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">{<\/span><br \/>\ntipo<span style=\"color: #339933\">:<\/span> <span style=\"color: #3366cc\">'sess\u00e3o'<\/span><span style=\"color: #339933\">,<\/span><br \/>\nlado<span style=\"color: #339933\">:<\/span> uuid.<span style=\"color: #660066\">v4<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">,<\/span><br \/>\nuid<span style=\"color: #339933\">:<\/span> uid<br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> sessDocName <span style=\"color: #339933\">=<\/span> <span style=\"color: #3366cc\">'sess-'<\/span> <span style=\"color: #339933\">+<\/span> sessDoc.<span style=\"color: #660066\">lado<\/span><span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>Em seguida, armazenamos nosso documento de sess\u00e3o rec\u00e9m-criado em nosso cluster. Voc\u00ea tamb\u00e9m notar\u00e1 que chamamos nossa fun\u00e7\u00e3o de sanitiza\u00e7\u00e3o acima aqui, bem como definimos um valor de expira\u00e7\u00e3o de 60 minutos. Isso far\u00e1 com que o cluster \"expire\" a sess\u00e3o, removendo-a ap\u00f3s esse per\u00edodo de tempo.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">db.<span style=\"color: #660066\">adicionar<\/span><span style=\"color: #009900\">(<\/span>sessDocName<span style=\"color: #339933\">,<\/span> sessDoc<span style=\"color: #339933\">,<\/span> <span style=\"color: #009900\">{<\/span>expira\u00e7\u00e3o<span style=\"color: #339933\">:<\/span> <span style=\"color: #cc0000\">3600<\/span><span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> resultado<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\nretorno de chamada<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> cleanSessionObj<span style=\"color: #009900\">(<\/span>sessDoc<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">,<\/span> resultado.<span style=\"color: #660066\">cas<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>Em seguida, precisamos criar uma fun\u00e7\u00e3o em nosso modelo que nos permita recuperar as informa\u00e7\u00f5es da sess\u00e3o que armazenamos anteriormente. Para fazer isso, geramos uma chave que corresponde \u00e0 que ter\u00edamos armazenado e, em seguida, executamos uma solicita\u00e7\u00e3o get, retornando o uid do usu\u00e1rio da sess\u00e3o para o nosso retorno de chamada.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">SessionModel.<span style=\"color: #660066\">obter<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>lado<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> sessDocName <span style=\"color: #339933\">=<\/span> <span style=\"color: #3366cc\">'sess-'<\/span> <span style=\"color: #339933\">+<\/span> lado<span style=\"color: #339933\">;<\/span>db.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>sessDocName<span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> resultado<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> retorno de chamada<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p>retorno de chamada<span style=\"color: #009900\">(<\/span><span style=\"color: #003366;font-weight: bold\">nulo<\/span><span style=\"color: #339933\">,<\/span> resultado.<span style=\"color: #660066\">valor<\/span>.<span style=\"color: #660066\">uid<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<\/div>\n<\/div>\n<p>E aqui est\u00e1 o sessionmodel.js finalizado:<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">var<\/span> db <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'.\/..\/database'<\/span><span style=\"color: #009900\">)<\/span>.<span style=\"color: #660066\">mainBucket<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> couchbase <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'couchbase'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> uuid <span style=\"color: #339933\">=<\/span> exigir<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'uuid'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span> cleanSessionObj<span style=\"color: #009900\">(<\/span>obj<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">excluir<\/span> obj.<span style=\"color: #660066\">tipo<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> obj<span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p><span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span> SessionModel<span style=\"color: #009900\">(<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p>SessionModel.<span style=\"color: #660066\">criar<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>uid<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> sessDoc <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">{<\/span><br \/>\ntipo<span style=\"color: #339933\">:<\/span> <span style=\"color: #3366cc\">'sess\u00e3o'<\/span><span style=\"color: #339933\">,<\/span><br \/>\nlado<span style=\"color: #339933\">:<\/span> uuid.<span style=\"color: #660066\">v4<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">,<\/span><br \/>\nuid<span style=\"color: #339933\">:<\/span> uid<br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> sessDocName <span style=\"color: #339933\">=<\/span> <span style=\"color: #3366cc\">'sess-'<\/span> <span style=\"color: #339933\">+<\/span> sessDoc.<span style=\"color: #660066\">lado<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<p>db.<span style=\"color: #660066\">adicionar<\/span><span style=\"color: #009900\">(<\/span>sessDocName<span style=\"color: #339933\">,<\/span> sessDoc<span style=\"color: #339933\">,<\/span> <span style=\"color: #009900\">{<\/span>expira\u00e7\u00e3o<span style=\"color: #339933\">:<\/span> <span style=\"color: #cc0000\">3600<\/span><span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> resultado<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\nretorno de chamada<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> cleanSessionObj<span style=\"color: #009900\">(<\/span>sessDoc<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">,<\/span> resultado.<span style=\"color: #660066\">cas<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<p>SessionModel.<span style=\"color: #660066\">obter<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>lado<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> sessDocName <span style=\"color: #339933\">=<\/span> <span style=\"color: #3366cc\">'sess-'<\/span> <span style=\"color: #339933\">+<\/span> lado<span style=\"color: #339933\">;<\/span><\/p>\n<p>db.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>sessDocName<span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> resultado<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> retorno de chamada<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p>retorno de chamada<span style=\"color: #009900\">(<\/span><span style=\"color: #003366;font-weight: bold\">nulo<\/span><span style=\"color: #339933\">,<\/span> resultado.<span style=\"color: #660066\">valor<\/span>.<span style=\"color: #660066\">uid<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<p>m\u00f3dulo.<span style=\"color: #660066\">exporta\u00e7\u00f5es<\/span> <span style=\"color: #339933\">=<\/span> SessionModel<span style=\"color: #339933\">;<\/span><\/p>\n<\/div>\n<\/div>\n<h1>Gerenciamento de sess\u00f5es - Pesquisa de contas<\/h1>\n<p>Antes de come\u00e7armos a escrever nosso manipulador de solicita\u00e7\u00f5es propriamente dito, precisamos criar um m\u00e9todo que nos permita procurar um usu\u00e1rio com base em seu nome de usu\u00e1rio. N\u00e3o queremos que os usu\u00e1rios tenham que se lembrar de seus IDs! Na parte 1, criamos nosso modelo de conta em accountmodel.js. Vamos voltar a esse arquivo e adicionar um novo m\u00e9todo.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">AccountModel.<span style=\"color: #660066\">getByUsername<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>nome de usu\u00e1rio<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/div>\n<\/div>\n<p>How we handle this method is that we will build a key using the provided username that will search for one of the referential documents we created in `AccountModel.create`. If we are not able to locate this referential document, we assume that the user does not exist and return an error. If the username is able to be located, and we find the referential document, we then execute a `AccountModel.get` to locate the user document itself, and forward the callback through there. This means that calls to `AccountModel.getByUsername` will return the full user object as if you had directly called `AccountModel.get` with the uid.<\/p>\n<p>Aqui est\u00e1 a fun\u00e7\u00e3o completa:<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">AccountModel.<span style=\"color: #660066\">getByUsername<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>nome de usu\u00e1rio<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> refdocName <span style=\"color: #339933\">=<\/span> <span style=\"color: #3366cc\">'nome de usu\u00e1rio-'<\/span> <span style=\"color: #339933\">+<\/span> nome de usu\u00e1rio<span style=\"color: #339933\">;<\/span><br \/>\ndb.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>refdocName<span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> resultado<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro <span style=\"color: #339933\">&amp;&amp;<\/span> err.<span style=\"color: #660066\">c\u00f3digo<\/span> <span style=\"color: #339933\">===<\/span> couchbase.<span style=\"color: #660066\">erros<\/span>.<span style=\"color: #660066\">keyNotFound<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> retorno de chamada<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'Nome de usu\u00e1rio n\u00e3o encontrado'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span> <span style=\"color: #000066;font-weight: bold\">mais<\/span> <span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> retorno de chamada<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #006600;font-style: italic\">\/\/ Extrair o UID que encontramos<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> foundUid <span style=\"color: #339933\">=<\/span> resultado.<span style=\"color: #660066\">valor<\/span>.<span style=\"color: #660066\">uid<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<p><span style=\"color: #006600;font-style: italic\">\/\/ Encaminhar para um get normal<\/span><br \/>\nAccountModel.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>foundUid<span style=\"color: #339933\">,<\/span> retorno de chamada<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<\/div>\n<\/div>\n<h1>Gerenciamento de sess\u00f5es - Tratamento de solicita\u00e7\u00f5es<\/h1>\n<p>A \u00faltima etapa da ativa\u00e7\u00e3o da cria\u00e7\u00e3o da sess\u00e3o \u00e9 escrever o pr\u00f3prio manipulador de solicita\u00e7\u00f5es. Felizmente, a maior parte da l\u00f3gica importante foi inclu\u00edda nas se\u00e7\u00f5es acima, e nosso manipulador de solicita\u00e7\u00f5es simplesmente encaminha as informa\u00e7\u00f5es para cada uma delas para processamento. Primeiro, validamos nossas entradas do usu\u00e1rio para garantir que tudo o que \u00e9 necess\u00e1rio foi fornecido. Em seguida, tentamos localizar a conta pelo nome de usu\u00e1rio fornecido. Em seguida, validamos se a senha corresponde ao que o usu\u00e1rio forneceu, fazendo o hash da senha fornecida e comparando-a. E, por fim, criamos a sess\u00e3o com nosso modelo de sess\u00e3o rec\u00e9m-criado e retornamos os detalhes ao usu\u00e1rio. Voc\u00ea pode notar que o ID da sess\u00e3o n\u00e3o \u00e9 fornecido diretamente ao usu\u00e1rio, mas, em vez disso, \u00e9 passado no cabe\u00e7alho. Isso \u00e9 para fins de consist\u00eancia, pois o cabe\u00e7alho tamb\u00e9m \u00e9 usado para autenticar cada solicita\u00e7\u00e3o posteriormente.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">aplicativo.<span style=\"color: #660066\">postagem<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'\/sessions'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>req<span style=\"color: #339933\">,<\/span> res<span style=\"color: #339933\">,<\/span> pr\u00f3xima<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span><span style=\"color: #339933\">!<\/span>req.<span style=\"color: #660066\">corpo<\/span>.<span style=\"color: #660066\">nome de usu\u00e1rio<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> res.<span style=\"color: #660066\">enviar<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #cc0000\">400<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #3366cc\">'Deve especificar um nome de usu\u00e1rio'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span><span style=\"color: #339933\">!<\/span>req.<span style=\"color: #660066\">corpo<\/span>.<span style=\"color: #660066\">senha<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> res.<span style=\"color: #660066\">enviar<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #cc0000\">400<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #3366cc\">'Deve especificar uma senha'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span>accountModel.<span style=\"color: #660066\">getByUsername<\/span><span style=\"color: #009900\">(<\/span>req.<span style=\"color: #660066\">corpo<\/span>.<span style=\"color: #660066\">nome de usu\u00e1rio<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> usu\u00e1rio<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> pr\u00f3xima<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p><span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>cripta.<span style=\"color: #660066\">sha1<\/span><span style=\"color: #009900\">(<\/span>req.<span style=\"color: #660066\">corpo<\/span>.<span style=\"color: #660066\">senha<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #339933\">!==<\/span> usu\u00e1rio.<span style=\"color: #660066\">senha<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> res.<span style=\"color: #660066\">enviar<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #cc0000\">400<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #3366cc\">'As senhas n\u00e3o correspondem'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p>sessionModel.<span style=\"color: #660066\">criar<\/span><span style=\"color: #009900\">(<\/span>usu\u00e1rio.<span style=\"color: #660066\">uid<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> sess\u00e3o<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> pr\u00f3xima<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/p>\n<p>res.<span style=\"color: #660066\">setHeader<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'Autoriza\u00e7\u00e3o'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #3366cc\">'Portador '<\/span> <span style=\"color: #339933\">+<\/span> sess\u00e3o.<span style=\"color: #660066\">lado<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<p><span style=\"color: #006600;font-style: italic\">\/\/ Excluir a senha por motivos de seguran\u00e7a<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">excluir<\/span> usu\u00e1rio.<span style=\"color: #660066\">senha<\/span><span style=\"color: #339933\">;<\/span><br \/>\nres.<span style=\"color: #660066\">enviar<\/span><span style=\"color: #009900\">(<\/span>usu\u00e1rio<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<\/div>\n<\/div>\n<p>Agora que nossa cria\u00e7\u00e3o de sess\u00e3o existe, vamos adicionar um m\u00e9todo para autenticar o usu\u00e1rio por solicita\u00e7\u00e3o. Por enquanto, coloquei esse m\u00e9todo no nosso app.js, mas talvez seja melhor coloc\u00e1-lo em um arquivo separado mais tarde, quando as rotas come\u00e7arem a ser separadas em arquivos distintos. Para autenticar o usu\u00e1rio, verificamos o cabe\u00e7alho HTTP Authorization padr\u00e3o, obtemos o ID da sess\u00e3o e o procuramos usando nosso modelo de sess\u00e3o. Se tudo correr como planejado, armazenamos o ID de usu\u00e1rio rec\u00e9m-encontrado na solicita\u00e7\u00e3o para os manipuladores de rotas posteriores.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\"><span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span> authUser<span style=\"color: #009900\">(<\/span>req<span style=\"color: #339933\">,<\/span> res<span style=\"color: #339933\">,<\/span> pr\u00f3xima<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\nreq.<span style=\"color: #660066\">uid<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #003366;font-weight: bold\">nulo<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>req.<span style=\"color: #660066\">cabe\u00e7alhos<\/span>.<span style=\"color: #660066\">autoriza\u00e7\u00e3o<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> authInfo <span style=\"color: #339933\">=<\/span> req.<span style=\"color: #660066\">cabe\u00e7alhos<\/span>.<span style=\"color: #660066\">autoriza\u00e7\u00e3o<\/span>.<span style=\"color: #660066\">dividir<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">&#8216; &#8216;<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>authInfo<span style=\"color: #009900\">[<\/span><span style=\"color: #cc0000\">0<\/span><span style=\"color: #009900\">]<\/span> <span style=\"color: #339933\">===<\/span> <span style=\"color: #3366cc\">\"Portador<\/span><span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #003366;font-weight: bold\">var<\/span> lado <span style=\"color: #339933\">=<\/span> authInfo<span style=\"color: #009900\">[<\/span><span style=\"color: #cc0000\">1<\/span><span style=\"color: #009900\">]<\/span><span style=\"color: #339933\">;<\/span><br \/>\nsessionModel.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>lado<span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> uid<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\npr\u00f3xima<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'Seu ID de sess\u00e3o \u00e9 inv\u00e1lido'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span> <span style=\"color: #000066;font-weight: bold\">mais<\/span> <span style=\"color: #009900\">{<\/span><br \/>\nreq.<span style=\"color: #660066\">uid<\/span> <span style=\"color: #339933\">=<\/span> uid<span style=\"color: #339933\">;<\/span><br \/>\npr\u00f3xima<span style=\"color: #009900\">(<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span> <span style=\"color: #000066;font-weight: bold\">mais<\/span> <span style=\"color: #009900\">{<\/span><br \/>\npr\u00f3xima<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'Deve ser autorizado a acessar esse endpoint'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><br \/>\n<span style=\"color: #009900\">}<\/span> <span style=\"color: #000066;font-weight: bold\">mais<\/span> <span style=\"color: #009900\">{<\/span><br \/>\npr\u00f3xima<span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'Deve ser autorizado a acessar esse endpoint'<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><\/div>\n<\/div>\n<p>Mainly for the purpose of displaying the authUser method in action I implemented a `\/me` endpoint that returns the user document. We simply do a get through the account model based on the uid that was stored into the request by the authInfo handler, and return this to the client, of course stripping away the password first.<\/p>\n<div class=\"geshifilter\">\n<div class=\"javascript geshifilter-javascript\" style=\"font-family: monospace\">aplicativo.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span><span style=\"color: #3366cc\">'\/me'<\/span><span style=\"color: #339933\">,<\/span> authUser<span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>req<span style=\"color: #339933\">,<\/span> res<span style=\"color: #339933\">,<\/span> pr\u00f3xima<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\naccountModel.<span style=\"color: #660066\">obter<\/span><span style=\"color: #009900\">(<\/span>req.<span style=\"color: #660066\">uid<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #003366;font-weight: bold\">fun\u00e7\u00e3o<\/span><span style=\"color: #009900\">(<\/span>erro<span style=\"color: #339933\">,<\/span> usu\u00e1rio<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">se<\/span> <span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span> <span style=\"color: #009900\">{<\/span><br \/>\n<span style=\"color: #000066;font-weight: bold\">retorno<\/span> pr\u00f3xima<span style=\"color: #009900\">(<\/span>erro<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #000066;font-weight: bold\">excluir<\/span> usu\u00e1rio.<span style=\"color: #660066\">senha<\/span><span style=\"color: #339933\">;<\/span><br \/>\nres.<span style=\"color: #660066\">enviar<\/span><span style=\"color: #009900\">(<\/span>usu\u00e1rio<span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><br \/>\n<span style=\"color: #009900\">}<\/span><span style=\"color: #009900\">)<\/span><span style=\"color: #339933\">;<\/span><\/p>\n<\/div>\n<\/div>\n<h1>Final<\/h1>\n<p>Neste ponto, voc\u00ea j\u00e1 deve ser capaz de criar contas, fazer login nelas e solicitar informa\u00e7\u00f5es armazenadas sobre o usu\u00e1rio. Aqui est\u00e1 um exemplo usando o usu\u00e1rio que criamos na Parte 1.<\/p>\n<div class=\"geshifilter\">\n<div class=\"text geshifilter-text\" style=\"font-family: monospace\">&gt; POST \/sessions<br \/>\n{<br \/>\n\"nome de usu\u00e1rio\": \"brett19\",<br \/>\n\"senha\": \"success!\"<br \/>\n}<br \/>\n&lt; 200 OK<br \/>\nCabe\u00e7alho (Autoriza\u00e7\u00e3o): Bearer 0e9dd36c-5e2c-4f0e-9c2c-bffeea72d4f7&gt; GET \/me<br \/>\nCabe\u00e7alho (Autoriza\u00e7\u00e3o): Bearer 0e9dd36c-5e2c-4f0e-9c2c-bffeea72d4f7<br \/>\n&lt; 200 OK<br \/>\n{<br \/>\n\"uid\": \u201cb836d211-425c-47de-9faf-5d0adc078edc\u201d,<br \/>\n\"nome\": \"Brett Lawson\",<br \/>\n\"nome de usu\u00e1rio\": \"brett19\"<br \/>\n}<\/p>\n<\/div>\n<\/div>\n<p>A fonte completa desse aplicativo est\u00e1 dispon\u00edvel aqui: <a href=\"https:\/\/github.com\/brett19\/node-gameapi\">https:\/\/github.com\/brett19\/node-gameapi<\/a><\/p>\n<p>Aproveite! Brett<\/p>","protected":false},"excerpt":{"rendered":"<p>In this part of the series, we will be implementing session management and authenticated endpoints (endpoints that require you to be logged in). Lets get started! If you have yet to read Part 1 of this series, I suggest you [&hellip;]<\/p>","protected":false},"author":31,"featured_media":13873,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"ppma_author":[9004],"class_list":["post-1615","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.5 (Yoast SEO v26.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Game Servers and Couchbase with Node.js - Part 2 - The Couchbase Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-2\/\" \/>\n<meta property=\"og:locale\" content=\"pt_BR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Game Servers and Couchbase with Node.js - Part 2\" \/>\n<meta property=\"og:description\" content=\"In this part of the series, we will be implementing session management and authenticated endpoints (endpoints that require you to be logged in). Lets get started! If you have yet to read Part 1 of this series, I suggest you [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-2\/\" \/>\n<meta property=\"og:site_name\" content=\"The Couchbase Blog\" \/>\n<meta property=\"article:published_time\" content=\"2014-12-16T19:32:03+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-28T07:23:52+00:00\" \/>\n<meta name=\"author\" content=\"Brett Lawson, Principal Software Engineer, Couchbase\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Brett Lawson, Principal Software Engineer, Couchbase\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/\"},\"author\":{\"name\":\"Brett Lawson, Principal Software Engineer, Couchbase\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/5cfc2fbf25776be2a027a474562be02f\"},\"headline\":\"Game Servers and Couchbase with Node.js &#8211; Part 2\",\"datePublished\":\"2014-12-16T19:32:03+00:00\",\"dateModified\":\"2023-06-28T07:23:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/\"},\"wordCount\":1386,\"commentCount\":10,\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"articleSection\":[\"Uncategorized\"],\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/\",\"name\":\"Game Servers and Couchbase with Node.js - Part 2 - The Couchbase Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"datePublished\":\"2014-12-16T19:32:03+00:00\",\"dateModified\":\"2023-06-28T07:23:52+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#breadcrumb\"},\"inLanguage\":\"pt-BR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png\",\"width\":1800,\"height\":630},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.couchbase.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Game Servers and Couchbase with Node.js &#8211; Part 2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#website\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"name\":\"The Couchbase Blog\",\"description\":\"Couchbase, the NoSQL Database\",\"publisher\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"pt-BR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#organization\",\"name\":\"The Couchbase Blog\",\"url\":\"https:\/\/www.couchbase.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"contentUrl\":\"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png\",\"width\":218,\"height\":34,\"caption\":\"The Couchbase Blog\"},\"image\":{\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/5cfc2fbf25776be2a027a474562be02f\",\"name\":\"Brett Lawson, Principal Software Engineer, Couchbase\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"pt-BR\",\"@id\":\"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/ee3586f0c112c20e863af447d44dec8f\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6aec1ba24ef7558a248dcde7b7a18b15b06e2885b24b663906a448634066c1c4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6aec1ba24ef7558a248dcde7b7a18b15b06e2885b24b663906a448634066c1c4?s=96&d=mm&r=g\",\"caption\":\"Brett Lawson, Principal Software Engineer, Couchbase\"},\"description\":\"Brett Lawson is a Principal Software Engineer at Couchbase. Brett is responsible for the design and development of the Couchbase Node.js and PHP clients as well as playing a role in the design and development of the C library, libcouchbase.\",\"url\":\"https:\/\/www.couchbase.com\/blog\/pt\/author\/brett-lawson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Servidores de jogos e Couchbase com Node.js - Parte 2 - The Couchbase Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-2\/","og_locale":"pt_BR","og_type":"article","og_title":"Game Servers and Couchbase with Node.js - Part 2","og_description":"In this part of the series, we will be implementing session management and authenticated endpoints (endpoints that require you to be logged in). Lets get started! If you have yet to read Part 1 of this series, I suggest you [&hellip;]","og_url":"https:\/\/www.couchbase.com\/blog\/pt\/game-servers-and-couchbase-nodejs-part-2\/","og_site_name":"The Couchbase Blog","article_published_time":"2014-12-16T19:32:03+00:00","article_modified_time":"2023-06-28T07:23:52+00:00","author":"Brett Lawson, Principal Software Engineer, Couchbase","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Brett Lawson, Principal Software Engineer, Couchbase","Est. reading time":"7 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#article","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/"},"author":{"name":"Brett Lawson, Principal Software Engineer, Couchbase","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/5cfc2fbf25776be2a027a474562be02f"},"headline":"Game Servers and Couchbase with Node.js &#8211; Part 2","datePublished":"2014-12-16T19:32:03+00:00","dateModified":"2023-06-28T07:23:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/"},"wordCount":1386,"commentCount":10,"publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","articleSection":["Uncategorized"],"inLanguage":"pt-BR","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/","url":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/","name":"Servidores de jogos e Couchbase com Node.js - Parte 2 - The Couchbase Blog","isPartOf":{"@id":"https:\/\/www.couchbase.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage"},"thumbnailUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","datePublished":"2014-12-16T19:32:03+00:00","dateModified":"2023-06-28T07:23:52+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#breadcrumb"},"inLanguage":"pt-BR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/"]}]},{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#primaryimage","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/sites\/1\/2022\/11\/couchbase-nosql-dbaas.png","width":1800,"height":630},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/blog\/game-servers-and-couchbase-nodejs-part-2\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Game Servers and Couchbase with Node.js &#8211; Part 2"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/blog\/#website","url":"https:\/\/www.couchbase.com\/blog\/","name":"Blog do Couchbase","description":"Couchbase, o banco de dados NoSQL","publisher":{"@id":"https:\/\/www.couchbase.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"pt-BR"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/blog\/#organization","name":"Blog do Couchbase","url":"https:\/\/www.couchbase.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","contentUrl":"https:\/\/www.couchbase.com\/blog\/wp-content\/uploads\/2023\/04\/admin-logo.png","width":218,"height":34,"caption":"The Couchbase Blog"},"image":{"@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/5cfc2fbf25776be2a027a474562be02f","name":"Brett Lawson, engenheiro de software principal, Couchbase","image":{"@type":"ImageObject","inLanguage":"pt-BR","@id":"https:\/\/www.couchbase.com\/blog\/#\/schema\/person\/image\/ee3586f0c112c20e863af447d44dec8f","url":"https:\/\/secure.gravatar.com\/avatar\/6aec1ba24ef7558a248dcde7b7a18b15b06e2885b24b663906a448634066c1c4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6aec1ba24ef7558a248dcde7b7a18b15b06e2885b24b663906a448634066c1c4?s=96&d=mm&r=g","caption":"Brett Lawson, Principal Software Engineer, Couchbase"},"description":"Brett Lawson \u00e9 engenheiro de software principal da Couchbase. Brett \u00e9 respons\u00e1vel pelo projeto e desenvolvimento dos clientes Node.js e PHP do Couchbase, al\u00e9m de desempenhar um papel no projeto e desenvolvimento da biblioteca C, libcouchbase.","url":"https:\/\/www.couchbase.com\/blog\/pt\/author\/brett-lawson\/"}]}},"authors":[{"term_id":9004,"user_id":31,"is_guest":0,"slug":"brett-lawson","display_name":"Brett Lawson, Principal Software Engineer, Couchbase","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/6aec1ba24ef7558a248dcde7b7a18b15b06e2885b24b663906a448634066c1c4?s=96&d=mm&r=g","author_category":"","last_name":"Lawson","first_name":"Brett","job_title":"","user_url":"","description":"Brett Lawson \u00e9 engenheiro de software principal da Couchbase. Brett \u00e9 respons\u00e1vel pelo projeto e desenvolvimento dos clientes Node.js e PHP do Couchbase, al\u00e9m de desempenhar um papel no projeto e desenvolvimento da biblioteca C, libcouchbase."}],"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts\/1615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/comments?post=1615"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/posts\/1615\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/media\/13873"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/media?parent=1615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/categories?post=1615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/tags?post=1615"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.couchbase.com\/blog\/pt\/wp-json\/wp\/v2\/ppma_author?post=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}