Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature 2 #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
Empty file added .eslintrc.js
Empty file.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
68 changes: 68 additions & 0 deletions expressServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"use strict";

const { response } = require("express");
const { application } = require("express");
const express = require("express");
const app = express();
application.use(express.json());

//require the path directory
var fs = require("fs");
var path = require("path");
var petsPath = path.join(__dirname, "pets.json");

//require the http module and create port
var port = 8000;

//handle the request with routes
app.get("/pets", (req, res) => {
//logic
fs.readFile(petsPath, "utf-8", function (err, petsJSON) {
if (err) {
console.log(err);
response.status(500);
response.send(err);
} else {
res.status(201);
res.send(petsJSON);
}
});
});

app.get("/pets/0", (req, res) => {
//logic
fs.readFile(petsPath, "utf8", function (err, petsJSON) {
if (err) {
console.log(err);
response.status(500);
response.send(err);
} else {
var pets = JSON.parse(petsJSON);
var petsData = JSON.stringify(pets[0]);

res.status(201);
res.send(petsData);
}
});
});

app.get("/pets/1", (req, res) => {
//logic
fs.readFile(petsPath, "utf8", function (err, petsJSON) {
if (err) {
console.log(err);
response.status(500);
response.send(err);
} else {
var pets = JSON.parse(petsJSON);
var petsData = JSON.stringify(pets[1]);
res.status(201);
res.send(petsData);
}
});
});

//listening on port
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
2 changes: 1 addition & 1 deletion fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Your first task is to build a command-line application that displays its usage,

```shell
$ node pets.js
Usage: node pets.js [read | create | update | destroy]
Usage: node pets.js [read | create | update | destroy]
```

Your next task is to refactor the application to handle the `read` subcommand via the [process arguments]['argv'], read the `pets.json` file, parse its data to a native JavaScript object, and log it to the console. If the call to the filesystem fails for any reason, it should throw the resulting error.
Expand Down
61 changes: 61 additions & 0 deletions httpServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
"use strict";
//require the path directory
var fs = require("fs");
var path = require("path");
var petsPath = path.join(__dirname, "pets.json");
//require the http module and create a port
var http = require("http");
var port = process.env.PORT || 8000;
//create the server instance
var server = http.createServer(function (req, res) {
if (req.method === "GET" && req.url === "/pets") {
fs.readFile(petsPath, "utf8", function (err, petsJSON) {
if (err) {
console.error(err.stack);
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain");
return res.end("Internal Server Error");
}
res.setHeader("Content-Type", "application/json");
res.statusCode = 200;
res.end(petsJSON);
});
} else if (req.method === "GET" && req.url === "/pets/0") {
fs.readFile(petsPath, "utf8", function (err, petsJSON) {
if (err) {
console.error(err.stack);
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain");
return res.end("Internal Server Error");
}
var pets = JSON.parse(petsJSON);
var guestJSON = JSON.stringify(pets[0]);
res.setHeader("Content-Type", "application/json");
res.statusCode = 200;
res.end(guestJSON);
});
} else if (req.method === "GET" && req.url === "/pets/1") {
fs.readFile(petsPath, "utf8", function (err, petsJSON) {
if (err) {
console.error(err.stack);
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain");
return res.end("Internal Server Error");
}
var pets = JSON.parse(petsJSON);
var guestJSON = JSON.stringify(pets[1]);
res.setHeader("Content-Type", "application/json");
res.statusCode = 200;
res.end(guestJSON);
});
} else {
res.setHeader("Content-Type", "text/plain");
res.statusCode = 404;
res.end("Not found");
}
});
server.listen(port, function () {
console.log("Listening on port", port);
});
//make the file available for export
module.exports = server;
Loading