Set up the Node environment and run the RESTful Api service locally

  • The course introduction can be found here: juejin.cn/post/684490…
  • Github address: github.com/hellozhangr…

Note: I wanted to talk about environment construction and API service in two chapters, but the operation is really too simple. But for the sake of the completeness of the series, let’s merge the two into one

Set up the Node environment locally

The development environment mainly refers to Node and MongoDB. I use a MAC computer, so the local environment is built according to the MAC.

Node

  1. The simplest way, directly from the official website. PKG file, graphical installation can be. nodejs.org/en/
  2. Go to Download for macOS (X64) and Download the stable version. The current version is 12.14.0lts
  3. Graphical installation automatically configures environment variables after installationnode -vVerify that the installation is successful.

MongoDB

To know before installation

  1. Check to see if mongodb has been installed on your Mac. If you prefer homebrew, check brew List to see if mongodb has been installed on brew. If it is to use homebrew loading, the default configuration file path is/usr/local/etc/mongod. Conf.

  2. If you don’t have mongodb installed, don’t install it with Homebrew as it no longer supports mongodb. Brew mongodb installation failed. For those of you interested in mongo gossip, is MongoDB going to cool down?

Reinstall mongodb

  1. downloadCD/usr/local && sudo curl - O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
  2. Unpack theSudo tar - ZXVF mongo osx - SSL - x86_64-4.0.9. TGZ
  3. Rename the mongodb directorySudo mv mongo osx - x86_64-4.0.9 / mongo
  4. Configure the PATHexport PATH=/usr/local/mongodb/bin:$PATHOr go to.base_profile and set path
    • And when that’s done,mongodYou can execute it. /usr/local/etc/mongo.conf and /user/local/var/mongo.log&data. We can simply configure these files to go to /user/local/mongodb
  5. configurationmongod.conf.
    • Create a new one under /user/local/mongodbmongod.confFile and write
    # Store data in /usr/local/mongodb/data instead of the default /data/db
    dbpath = /usr/local/mongodb/data
    
    # Append logs to /usr/local/mongodb/log/mongo.log
    logpath = /usr/local/mongodb/log/mongo.log
    logappend = true
    
    # Only accept local connectionsBind_ip = 127.0.0.1Copy the code
    • A data directory specified in mongod. Conf, and a mongo.log file to be created manually.
  6. Start the Mongod servicemongod --config /usr/local/mongodb/mongod.conf
  7. Enter the Mongo databasemongo

The content of this chapter is very similar to the content of Article 5. Please refer to deploying the project to Tencent Cloud Server

Use the Express RESTful Api service

Simple API demo

  1. Install express package:npm install --save express
  2. Create a service with Express, filenameapp.js
const express = require('express');
const app = express();
app.use('/api', (req, res, next) => {
    res.json('hello i am api');
});
app.listen('3000', () = > {console.log('listen: 3000');
});
Copy the code
  1. Start the file with Nodenode app.js, enter in the browserlocalhost:3000/apiYou’ll see.

Express is very easy to use, go to the official website or any website to find an Express service. I was afraid I’d be laughed at for being too simple. Don’t rush. Take your time. More exciting things will unfold gradually.