[Re-learn Node.js 1 & 2] the local environment and to build a Node from RESTful Api Service

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

  1. The easiest way, directly from the official website .pkg files, graphical install. https://nodejs.org/en/
  2. Find Download for macOS (x64), download the stable version, the current version is: 12.14.0 LTS
  3. Graphical installation will automatically configure the environment variables, Bahrain node -vverify that the installation was successful.

MongoDB

To learn before installation

  1. First confirm whether their installed over the next Mac, if they get used to homebrew, brew listto see if a brew installed over mongodb. If you are using homebrew installed, the default configuration file path /usr/local/etc/mongod.conf.

  2. 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

  1. download
    cd /usr/local && sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
  2. Decompression
    sudo tar -zxvf mongodb-osx-ssl-x86_64-4.0.9.tgz
  3. Rename the directory mongodb
    sudo mv mongodb-osx-x86_64-4.0.9/ mongodb
  4. PATH configuration
    export PATH=/usr/local/mongodb/bin:$PATH
    or path is provided to .base_profile
    • Once this is accomplished, mongodit 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
  5. Configuration mongod.conf.
    • In / user / local / mongodb new mongod.conffile 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.
  6. Start mongod Service mongod --config /usr/local/mongodb/mongod.conf
  7. 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

  1. Express install package:npm install --save express
  2. 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');
});
  1. Start the file with the Node node app.js, enter in your browser localhost:3000/apiyou 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.

Guess you like

Origin www.cnblogs.com/zhangran/p/12128414.html