- Download and install Node.js
- Download and extract the Sample Project
- In a Terminal window, navigate to the extracted folder
- Run
npm install
- Run the app
This guide is meant to be a very quick setup for a Node.js REST API. It will include links to each component if you'd like to read more.
Note that I use macOS, so the commands may be slightly different on a non-Unix machine.
Visual Studio Code is an awesome IDE with built in support for JavaScript, TypeScript, and Node.js debugging. This is completely optional, but it's the best free environment I've used.
Recommended Extensions
Download and install Node.js if it isn't already installed. You can run node --version
in Terminal to verify installation.
Note that
npm
is packaged withNode.js
. Runnpm --version
to verify.
TypeScript is optional, but I highly recommend using it for developing type-safe JavaScript applications.
- Install TypeScript by running
npm install -g typescript
- Run
tsc --version
to verify installation.
Compile TypeScript files by running tsc
for all TypeScript files, or tsc file.ts
for a single file.
-
Create your project's directory folder by running
mkdir my-app-name && cd my-app-name
-
Initialize npm by running
npm init
and going through the prompts.Note that the
package.json
file that's created stores your app's dependencies. On a new computer, dependencies can be installed by runningnpm install
from the same directory. -
If your IDE supports Node.js, install Node.js types:
npm install @types/node --save
-
If using TypeScript, run
tsc --init
and excludenode_modules
intsconfig.json
{ ... "exclude": [ "node_modules" ] }
This will ensure all Node dependencies are excluded from the
tsc
command.
Express makes it incredibily easy to create a REST API in JavaScript.
Install express npm install express --save
Note that the
--save
flag is what save's it to your project's dependencies.
supertest used for unit testing server behaviour.
Install by running npm install supertest --save-dev
Mocha is a great JavaScript unit testing framework. I highly recommend it.
- Install by running
npm install mocha --save-dev
- If your IDE supports Node.js, install Mocha types:
npm install @types/mocha --save-dev
- Add the following to
package.json
"scripts" : { "test: "mocha" }
- Add a test directory
mkdir test
- Running
npm test
will give an error saying there are no test files found (expected).
Note that by default, Mocha will run all JavaScript files in the
test/
directory.
Add the following files to your project:
app.js
var express = require('express')
var app = express()
app.get('/', function (req, res) {
res.send('Hello World!\n')
})
// Export server for testing.
var server = app.listen(3000);
module.exports = server;
test/app_tests.js
var request = require("supertest");
var assert = require('assert');
describe('array', function() {
describe('#indexOf()', function() {
it('should return -1 when the value is not present', function() {
assert.equal(-1, [1,2,3].indexOf(4));
});
});
});
describe('routes', function() {
var server;
beforeEach(function() {
// Clears the cache so a new server instance is used for each test.
delete require.cache[require.resolve('../app')];
server = require("../app");
});
afterEach(function() {
server.close();
});
// Test to make sure URLs respond correctly.
it("url /", function(done) {
request(server)
.get("/")
.end(function(err, res) {
assert.equal(res.text, "Hello World!\n");
done();
});
});
});
- Run
node app.js
- Run
curl localhost:3000
in a new Terminal window. Hello World!
will print in the console.
- If using TypeScript, run
tsc
on your project's root directory to compile all TypeScript files. - Run
npm test
. The following will print in the Terminal window:> [email protected] test /Users/Cohen/node-app > mocha array #indexOf() ✓ should return -1 when the value is not present routes ✓ url / 2 passing (141ms)
I strongly encourage your to read the documentation for the components used in the guide (see Components). They are all excellent and extremely easy to follow.