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
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"rules": {
"no-console": "off",
"indent": [ "error", 2 ],
"quotes": [ "error", "single" ],
"semi": ["error", "always"],
"linebreak-style": [ "error", "unix" ]
},
"env": {
"es6": true,
"node": true,
"mocha": true,
"jasmine": true
},
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true,
"impliedStrict": true
},
"extends": "eslint:recommended"
}
116 changes: 116 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@

# Created by https://www.gitignore.io/api/node,macos,windows,linux

### Node ###
# Logs
logs
*.log
npm-debug.log*
node_modules
coverage

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity



### macOS ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon
# Thumbnails
._*
# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent
# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk


### Windows ###
# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk


### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Vanilla HTTP Server

This project creates a local http server and allows users send GET and POST requests through the terminal.
When sending GET and POST requests with filepath ```/cowsay``` with a query, a response will return with
a cute cow.

## How to run

Install any Dependencies from the ```package.json``` file into the project root
directory. Using node, you can run the command ```npm i``` to install all
depenenedcies.

## Running server

Run the ```server.js``` file using command ```node server.js```. You should see ```Server running on PORT: 8000``` in terminal.

## Sending GET and POST Request

>GET Request

In an new terminal window, send a GET request by using the command ```http localhost:8000```. To receive a response with a cow, include
```/cowsay``` and a text query. Example: ```http localhost:8000/cowsay?text='hello there'```.
```
-------------
< hello there >
-------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||

```

>Post Request

In an new terminal window, send a POST request by using the command ```http POST localhost:8000```. To receive a response with a cow,
include ```/cowsay``` and a text message. Example: ```http localhost:8000/cowsay text='hello there'```.

>One cool thing

In a GET request, use ```dragon``` for your text query.


## Closing server

In server terminal, enter ```control``` + ```c```.
37 changes: 37 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

const gulp = require('gulp');
const eslint = require('gulp-eslint');
const mocha = require('gulp-mocha');
const cache = require('gulp-cache');
const istanbul = require('gulp-istanbul');

gulp.task('pre-test', function() {
return gulp.src(['./lib/*.js', './model/*.js', '!node_modules/**'])
.pipe(istanbul())
.pipe(istanbul.hookRequire());
});

gulp.task('test', ['pre-test'], function() {
gulp.src('./test/*-test.js', { read: false})
.pipe(mocha({ report: 'spec'}))
.pipe(istanbul.writeReports())
.pipe(istanbul.enforceThresholds({thresholds: {global: 90}}));
});

gulp.task('lint', function() {
return gulp.src(['**/*.js', '!node_modules/**'])
.pipe(eslint())
.pipe(eslint.format())
.pipe(eslint.failAfterError());
});

gulp.task('dev', function() {
gulp.watch(['**/*.js', '!node_modules/**'], ['lint', 'test']);
});

gulp.task('default', ['dev']);

gulp.task('clear', function (done) {
return cache.clearAll(done);
});
18 changes: 18 additions & 0 deletions lib/parse-body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'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);
}
});
};
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "07-vanilla-http-server",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/peterkim2/07-vanilla-http-server.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/peterkim2/07-vanilla-http-server/issues"
},
"homepage": "https://github.com/peterkim2/07-vanilla-http-server#readme",
"dependencies": {
"cowsay": "^1.1.9"
},
"devDependencies": {
"gulp": "^3.9.1",
"gulp-cache": "^0.4.5",
"gulp-eslint": "^3.0.1",
"gulp-istanbul": "^1.1.1",
"gulp-mocha": "^3.0.1"
}
}
55 changes: 55 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'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 || 8000;

const server = http.createServer(function(req, res) {
req.url = url.parse(req.url);
req.url.query = querystring.parse(req.url.query);

if(req.method === 'POST' && req.url.pathname === '/cowsay') {
parseBody (req, function(err) {
console.log('POST request body:', req.body);
if(err) console.error(err);
try {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: req.body.text}));
} catch(err){
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'bad request'}));
}
res.end();
});
return;
}

if(req.method === 'GET' && req.url.pathname === '/') {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('hello from my server!');
}

if(req.method === 'GET' && req.url.pathname === '/cowsay' && req.url.query) {
if(req.url.query.text ==='dragon') {
res.write(cowsay.say({f: 'dragon', text: 'OH SHOOT...DRAAAAAAGON!!'}));
res.end();
} else{
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: req.url.query.text}));
res.end();
}
}
else {
res.writeHead(400, {'Content-Type': 'text/plain'});
res.write(cowsay.say({text: 'bad request'}));
}
res.end();
return;
});

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