Hands-On Full-Stack Web Development with GraphQL and React
上QQ阅读APP看书,第一时间看更新

Running Express.js in development

To launch our server, we have to add a new script to our package.json.

We will add the following line to the scripts property of the package.json file:

"server": "nodemon --exec babel-node --watch src/server src/server/index.js"

As you can see, we are using a command called nodemon. We need to install it first:

npm install --save nodemon

Nodemon is an excellent tool for running a Node.js application. It can restart your server when the source changes.

For example, to get the above command working follow the steps below:

  1. Furthermore, we must install the @babel/node package, because we are transpiling the back end code with Babel, using the --exec babel-node option. It allows the use of the import statement:
npm install --save-dev @babel/node

Providing --watch as the option following a path or file will permanently track changes on that file or folder and reload the server to represent the latest state of your application. The last parameter refers to the actual file being the starting execution point for the back end.

  1. Start the server now:
npm run server

When you now go to your browser and enter http://localhost:8000, you will see the text Hello World! from our Express.js callback function.

Chapter 3Connecting to the Database, covers how Express.js routing works in detail.