使用Nodejs-Express-MongoDB搭建服务器
Demo source code on the github: Github demo source code.
Steps to start the server:
- Clone the Github demo source code.
- install MongoDB with the steps from Mac OSX 平台 MongoDB 的安装及管理.
- Config MongoDB and start it with
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
. - Start the server by running
node server.js
. - Test the APIs by curl or Postman (recommended).
Note
nodejs version
If you meet the following error while running node server.js
, check your nodejs version to make sure you are using 12 and above. 14.17.6
is tested. The error is:
1 2 3 |
|
How to start/shutdown MongoDB
- After correctly configuring your MongoDB, use the following command to start it:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
- After enter the mongo environment by running
mongo
, use the following command to shutdown the MongoDB: ```use admin; db.shutdownServer(); ```
MongoDB Compass
Use the official UI tool to manage your MongoDB data: MongoDB Compass.
Postman
Use the Postman to act as a client. It is convenient to config/manage the request.
code explanation
Refer to Node.js, Express & MongoDb: Build a CRUD Rest Api example for detailed code explanation.