Local environment and to build a Node from RESTful Api Service
Course description see here: https://www.cnblogs.com/zhangran/p/11963616.html
project github Address: https://github.com/hellozhangran/happy-egg-server
Description: This two chapters want to talk about the environment and to build from api service, but the operation down really too simple. But taking into account the integrity of the entire series can not speak, it would put the two into one
Local Node built environment
Development environment mainly refers to the Node, MongoDB. I am a mac computer use, so the local environment is built in accordance with mac ride.
Node
- The easiest way, directly from the official website .pkg files, graphical install. https://nodejs.org/en/
- Find Download for macOS (x64), download the stable version, the current version is: 12.14.0 LTS
- Graphical installation will automatically configure the environment variables, Bahrain
node -v
verify that the installation was successful.
MongoDB
To learn before installation
First confirm whether their installed over the next Mac, if they get used to homebrew,
brew list
to see if a brew installed over mongodb. If you are using homebrew installed, the default configuration file path/usr/local/etc/mongod.conf
.If not installed mongodb, do not use homebrew installation, because now no longer supported mongodb a homebrew. The reason I also said in the readme, concrete can see here brew mongodb installation error . Interested in gossip about the mongo may have a look here tragically abandoned Red Hat, MongoDB to cool it?
Reinstall mongodb
- download
cd /usr/local && sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
- Decompression
sudo tar -zxvf mongodb-osx-ssl-x86_64-4.0.9.tgz
- Rename the directory mongodb
sudo mv mongodb-osx-x86_64-4.0.9/ mongodb
- PATH configuration
export PATH=/usr/local/mongodb/bin:$PATH
or path is provided to .base_profile- Once this is accomplished,
mongod
it can be executed. Many tutorials will be configured in / usr / local / etc in mongo.conf files, configuration mongo.log & data files in the / user / local / var, although I discovered use, often can not find. We can under simple configuration, these files into / user / local / mongodb below
- Once this is accomplished,
- Configuration
mongod.conf
.- In / user / local / mongodb new
mongod.conf
file and writes
# 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 connections bind_ip = 127.0.0.1
- mongod.conf specified in a data directory, a mongo.log file to manually create their own under.
- In / user / local / mongodb new
- Start mongod Service
mongod --config /usr/local/mongodb/mongod.conf
- Enter mongo database
mongo
In this chapter the first five content is very similar, refer to
deploy the project to Tencent cloud server
Express service from using RESTful Api
Simple api demo
- Express install package:
npm install --save express
- Create a service with the express, file name
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');
});
- Start the file with the Node
node app.js
, enter in your browserlocalhost:3000/api
you will see.
express very simple to use, you can play a express service Quguan network or any Web site a look. I write this I am worried that we will be Tucao too simple. Do not worry, come slowly, it will gradually appear more exciting.