Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"text": "This is text"
}
Empty file added gulpfile.js
Empty file.
16 changes: 16 additions & 0 deletions lib/parse-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'use strict';

module.exports = function(req, callback){
req.body = '';
req.on('data', function(data){
req.body += data.toString();
});
req.on('end', function(){
try {
req.body = JSON.parse(req.body);
callback(null, req.body);
} catch (err) {
callback(err);
}
});
};
23 changes: 23 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "lab-07-cowsay-http-server",
"version": "1.0.0",
"description": "![cf](https://i.imgur.com/7v5ASc8.png) lab-07-cowsay-http-server ======",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/loganlsr/lab-07-cowsay-http-server.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/loganlsr/lab-07-cowsay-http-server/issues"
},
"homepage": "https://github.com/loganlsr/lab-07-cowsay-http-server#readme",
"dependencies": {
"cowsay": "^1.1.8"
}
}
62 changes: 62 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const http = require('http');
const url = require('url');
const queryString = require('querystring');

const cowsay = require('cowsay');
const parseBody = require('./lib/parse-body.js');

const PORT = process.env.PORT || 3000;

const server = http.createServer(function(req,res){
req.url = url.parse(req.url);
req.url.query = queryString.parse(req.url.query);
console.log('req.url', req.url);
console.log('req.query', req.query);
console.log('req.method', req.method);
console.log('req.headers', req.headers);

if (req.method === 'POST' && req.url.pathname === '/cowsay'){
parseBody(req, function(err, body){
if (err) {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'bad request\n Callback error'}));
res.end();
}
if(!req.body.text){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'bad request\ntry: Must have valid text property in JSON'}));
res.end();
}
if(req.body.text){
console.log(body);
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: req.body.text, f: req.body.f}));
res.end();
}
});
}

if (req.method === 'GET' && req.url.pathname === '/'){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'hello world'}));
res.end();
}

if (req.method === 'GET' && req.url.pathname === '/cowsay' && req.url.query.text){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: req.url.query.text, f: req.url.query.f}));
res.end();
}

if (req.method === 'GET' && req.url.pathname === '/cowsay' && !req.url.query.text){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'bad request\ntry: localhost:3000/cowsay?text=howdy'}));
res.end();
}
});

server.listen(PORT, function(){
console.log('Server running on PORT: ', PORT);
});