Skip to content

Commit

Permalink
Add method to parse query string
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueLimas committed May 8, 2017
1 parent 4604625 commit 8183cc6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ const through = require('through2')
const chatModel = require('./models/chats.js')
const chatHandler = require('./handlers/chat.js')

const url = require('url')
const querystring = require('querystring')

const server = http.createServer(function (req, res) {
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:9966')

if (/GET/i.test(req.method) && /chats/.test(req.url)) {
const username = require('url').parse(req.url).query.split('=')[1]
const username = query(req).user

chatModel.getAllChatsByUsername(username)
.then(function (chats) {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(chats))
})
} else if (/POST/i.test(req.method) && /chats/.test(req.url)) {
const username = require('url').parse(req.url).query.split('=')[1]
const username = query(req).user

chatModel.createChat(username)
.then(function (chat) {
Expand All @@ -28,6 +31,10 @@ const server = http.createServer(function (req, res) {
}
})

function query(req) {
return querystring.parse(url.parse(req.url).query)
}

const ws = websock.createServer({ server: server }, function (stream) {
stream.pipe(handleChat(stream))

Expand Down

0 comments on commit 8183cc6

Please sign in to comment.