Henrik Gustafsson

View Original

Hello Node: Getting started with Node.js and React - Part 1

Introduction

Recently I had the need to set up a small internal static site that interacted with a few remote services. I figured this to be an excellent opportunity to learn a bit about Node.js. This will be a three-part series setting up a Node.js/Express/React application.

My template/learning project is available on GitHub. I am a beginner at both the language(s) and the stack. Suggestions for improvements are welcome.

To follow along at home you will need to be running something reasonably UNIXy that supports Docker such as OSX or Linux. All these tools are available in Windows as well, but I can't help you with that. You will also need gitcurldocker, Node.js and npm installed.

Initial project structure

First up is setting up the git repository. I have opted for the following file structure:

See this content in the original post

Preparations

First, create the git repository to house our things:

See this content in the original post

The .gitignore file contains a list of patterns of files that should be ignored by git. Normally I like to create it by hand, but in the interest of learning new things I used the gitignore.io service to generate it.

The server

Next up is to create the actual server:

$ mkdir -p server/server
$ cd server

Fire up your favorite editor and create server/package.json:

See this content in the original post

You can also use npm init to generate the template and modify to suit. The server will use Express, so install it:

$ npm install --save --save-bundle express

The --save and --save-bundle options adds the package to the "dependencies" and "bundledDependencies" sections of package.json.

Create server/server/index.js:

See this content in the original post

Add the start script to package.json:

See this content in the original post

You should now have a functional server! To try it out, from the server/ directory, run npm start:

See this content in the original post

Try visiting http://localhost:8080/ in your browser or use curl:

$ curl http://localhost:8080/
Hello Node

Stop the server by pressing Ctrl-C.

Time to commit!

See this content in the original post

Testing

For no particular reason, I guess Mocha and Chai with the chai-http plugin would do for testing:

$ npm install --save-dev mocha chai chai-http

Create server/test/test-server.js:

See this content in the original post

Add the test script to our package.json, which should now look a bit like this:

See this content in the original post

Run the test with npm test!

See this content in the original post

Fancy! Let's commit this too.

See this content in the original post

Strunk and White

Let's make sure our code meets some standard. Which one doesn't really matter as long as it's consistent. For this, I use ESLint configured with the StandardJS configuration.

See this content in the original post

Add the eslint command to the npm test script:

See this content in the original post

Running it will reveal problems in the server test:

See this content in the original post

The first two issues is because the tests use symbols used by the testing framework, and the third is the lack of error handling. In general it's good practice, but in the tests we don't really want to pollute our code with such things.

Create a server/test/.eslintrc.json to override this:

See this content in the original post

Let's try again:

See this content in the original post

This is good!

See this content in the original post

Conclusion

We now have a very minimal web server with testing, style checking and code analysis. This is a good starting point for future endeavors.

Next up is adding the client side of things.

See this content in the original post