In order to create a best practice project creation for native Node.js projects, let us discuss a todo list for that.
README.md. Give it a good
projectname. One word, remember! For demo let us use
myg52. (re teachers NPM Create project)
$ git clone https://phidip@bitbucket.org/phidip/myg52.git Cloning into 'myg52'... remote: Counting objects: 3, done. remote: Compressing objects: 100% (2/2), done. remote: Total 3 (delta 0), reused 0 (delta 0) Unpacking objects: 100% (3/3), done.
$ cd myg52
$ npm init -y
Wrote to /home/nml/nodeMyGPNode/myg52/package.json:
{
"name": "myg52",
"version": "0.9.0",
"description": "**Edit a file, create a new file, and clone from Bitbucket in under 2 minutes**",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://phidip@bitbucket.org/phidip/myg52.git"
},
"keywords": [],
"author": "Niels Müller Larsen <nmla@iba.dk> (http://dkexit.eu)",
"license": "MIT",
"homepage": "https://bitbucket.org/phidip/myg52#readme"
}
main value to
main.js if that is your custom. Our
textbook seems to prefer that. Let's humor it.
Notice the repository object, and
the homepage property.
$ npm test > myg52@0.9.0 test /home/nml/nodeMyGPNode/myg52 > echo "Error: no test specified" && exit 1 Error: no test specified npm ERR! Test failed. See above for more details.Notice that it tries to run the command from the
scripts object of package.json.
scripts object of package.json so
that it holds:
"scripts": {
"checkpoint": "git add . && git commit -F - && git push origin master ",
"pretest": "npm install",
"test": "node main"
},
npm i <modulename>
eg
npm i http-status-codes
$ npm test
Generally, npm requires
$ npm run <script>
in order to run scripts mentioned in
the scripts object of the
package.json.
In this case it will execute
node main.
npm test belongs to a series of
npm scripts that may be run without the run
directive.
The checkpoint script must use
npm run checkpoint
to execute properly.
Whether you have to start the server
by npm test, or node main might
be more or less the same number of keystrokes, but the
added functionality is that before test is run,
pretest is run automatically. In this case
it will update all necessary npm modules to
their latest state according to the dependencies
object in package.json.
myg52/main.js
The following code is similar to listing 5.1 from your textbook augmented with listings 5.2 and 5.3.
"use strict";
const http = require("http");
const httpStatus = require("http-status-codes");
const hostname = "127.0.0.1";
const port = 3000;
const app = http.createServer(); // server as an obj
const getJSONString = function (obj) { // prettyprint obj
return JSON.stringify(obj, null, 4);
}
app.on("request", function (req, res) { // eventhandler for "request"
console.log("Log: Received an incoming request!");
console.log("Log: Method: " + req.method);
console.log("Log: URL: " + getJSONString(req.url));
console.log("Log: Headers:\n" + getJSONString(req.headers));
// prep response header
res.writeHead(httpStatus.OK, {
"Content-Type": "text/html; charset=utf-8"
});
// prep response body
let responseMsg = "<h1>Kilroy was here!</h1>";
responseMsg += "<p><kbd>myg52</kbd> at your disposal</p>";
res.write(responseMsg); // respond
res.end(); // sends response http
});
app.listen(port, hostname, function () {
console.log(`Server running, and listening at http://${hostname}:${port}/`);
});
On your CLI do npm test. Then go to your
browser and do
http://localhost:3000.
Check the server's log.