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
- The simplest way, directly from the official website. PKG file, graphical installation can be. nodejs.org/en/
- Go to Download for macOS (X64) and Download the stable version. The current version is 12.14.0lts
- Graphical installation automatically configures environment variables after installation
node -v
Verify that the installation is successful.
MongoDB
To know before installation
-
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.
-
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
- download
CD/usr/local && sudo curl - O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
- Unpack the
Sudo tar - ZXVF mongo osx - SSL - x86_64-4.0.9. TGZ
- Rename the mongodb directory
Sudo mv mongo osx - x86_64-4.0.9 / mongo
- Configure the PATH
export PATH=/usr/local/mongodb/bin:$PATH
Or go to.base_profile and set path- And when that’s done,
mongod
You 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
- And when that’s done,
- configuration
mongod.conf
.- Create a new one under /user/local/mongodb
mongod.conf
File 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.
- Create a new one under /user/local/mongodb
- Start the Mongod service
mongod --config /usr/local/mongodb/mongod.conf
- Enter the Mongo database
mongo
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
- Install express package:
npm install --save express
- Create a service with Express, filename
app.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
- Start the file with Node
node app.js
, enter in the browserlocalhost:3000/api
You’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.