A cookbook for getting started with node.js development fast!
MIT License
A cookbook for getting started with node.js development fast! It's based on the excellent tutorials written by Robin Wieruch.
If you're a Java developer and want to start developing with node.js, you've come to the right place... maybe. ;-)
There are 3 versions of the app that can be run:
npm run start-stub-nonmod
- Run Express app with stubbed model and non-modular routes.npm run
- Runs Express app with stubbed model and modular routes.npm start
- Runs Express app with MongoDB backed model and modular routes.Call the endpoints, e.g.
http://localhost:3000/users/1
http://localhost:3000/messages/1
Node Package Manager is used to manage node.js dependencies. A bit like Maven and Gradle.
The package.json
contains the project's dependency info.
npm uses semantic versioning for dependency management:
Run npm install
to install all the dependencies in package.json
- do after checking out of Git.
Run npm outdated
then npm update
and to check if we're up to date.
npm install express --save
- installs and updates prod deps in package.json
.
npm install jest --save
- installs and updates dev deps package.json
.
Runs the app in the background when you're developing, so that changes to the code are reflected immediately.
npm install nodemon --save-dev
Update pacakge.json
script:
"scripts": {
"start": "nodemon src/index.js",
Used to transpile latest JavaScript into simpler JS for node.js engine to execute. Allows developers to use latest ECMAScript features and run them on node.js.
npm install @babel/core @babel/node --save-dev
Update your pacakge.json
scripts start line with:
"scripts": {
"start": "nodemon --exec babel-node src/index.js",
Next, you need to tell which features (presets) of ECMAScript to use:
npm install @babel/preset-env --save-dev
Create a .babelrc file in project root and add:
{
"presets": [
"@babel/preset-env"
]
}
A very popular HTTP server for node.js. Excellent support for creating REST endpoints. Bit like Spring REST Coontrollers.
Essentially a REST endpoint.
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
Bit like servlet filters in the Java world.
Can be 3rd party libs or custom impl. Applied at the application level.
For example, to allow CORS for entire app (adds CORS header). Instead of adding it for every route, it can be done once at the app level.
npm install cors
import cors from 'cors';
const app = express();
app.use(cors());
Another example is well-known middleware for parsing HTTP POST requests into JSON:
npm install body-parser
import bodyParser from 'body-parser';
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
Runs only for a specific route.
TODO: Include example...
Use dotenv to access them. Store your env vars in a .env
file in project root folder.
npm install dotenv
import 'dotenv/config';
console.log(`Example app listening on port ${process.env.PORT}!`),
The app uses MongoDB for the database.
The app uses Mongoose for ORM (Onject Relation Mapping) mapping layer.
npm install mongoose --save
It maps a JavaScript object model to a MongoDB schema. The model can then be used by the routes to fetch users, delete messages, etc.
Use curl on the linux command line. Responses can be piped into Bash jq for formatting.
Example curl requests:
curl http://localhost:3000/messages | jq; # defaults to GET
curl -X DELETE http://localhost:3000/messages/msg_123
curl -X POST -H "Content-Type:application/json" http://localhost:3000/messages -d '{"text":"Hi again, World"}'
Use Postman.
VS Code is a decent free IDE for JavaScript development. Some useful extensions include: