- Web Development with MongoDB and NodeJS(Second Edition)
- Mithun Satheesh Bruno Joseph D'mello Jason Krol
- 403字
- 2021-07-09 21:13:01
Writing your first app
Now that you have everything installed and confirmed that it's all working, you can write your first quick app that will use both Node and MongoDB. This will prove that your environment is good to go, and you're ready to get started. In addition, it will give you a brief taste of the world of Node and MongoDB development! Don't worry if a lot of the following is confusing or just doesn't make sense to you—it will all be made clear throughout the rest of the book!
To begin with, we need to create a folder for our application where this application's specific code will reside, as follows:
$ mkdir testapp $ cd testapp
Creating the sample application
The testapp
folder that we just created will be the root of our sample Node application. Even though it's not necessary, it's important and also a best practice that we should create the package.json
file for our Node app, which will hold the necessary data regarding the application such as its version, name, description and development, and runtime dependencies. This can be done by issuing the following command from the testapp
folder root:
$ npm init
This command will follow up by asking you a few questions such as the name and version number of your newly created application. It is not necessary that you fill in all the details in one go, and you can skip the steps by pressing Enter and the default values will be entered, which you may update later.
Getting the dependent modules in place
Before we start to write any Node.js code, we need to get our dependencies in place by using npm. Since this is a basic app, we will be using it to test our Node.js connectivity with the MongoDB server. So, the only dependent module that we need is the native MongoDB client for Node.js. We can easily install that by executing the following command:
$ npm install mongodb --save
After npm installs the MongoDB driver, you can list the contents of the directory and notice that a new folder was created, node_modules
. This is where, surprisingly enough, all Node modules are stored whenever you install them from npm. Inside the node_modules
folder should be a single folder named mongodb
. Also, you will notice that the package.json
file of our sample application will be updated by this new dependency entry.
- UML+OOPC嵌入式C語言開發精講
- Linux操作系統基礎案例教程
- RISC-V體系結構編程與實踐(第2版)
- MATLAB 2020從入門到精通
- C語言程序設計
- 區塊鏈底層設計Java實戰
- 小程序,巧應用:微信小程序開發實戰(第2版)
- Unity 2018 Augmented Reality Projects
- Building Slack Bots
- MATLAB 2020 GUI程序設計從入門到精通
- jQuery Mobile Web Development Essentials(Second Edition)
- 詩意的邊緣
- C語言程序設計實驗指導教程
- Visual C++ 開發從入門到精通
- 微信小程序開發圖解案例教程:附精講視頻(第3版)