Here is a good introductory article on Node.js great nodejs intro , which will give you a very convenient introduction to Node.js and CouchDB, and give you an example to implement REST services for Perform CRUD operations on bookmarks, using CouchDB as the database.
This article will introduce how to install and start using Node.js under Mac OS X. This process will take about 30 minutes. We will also install CouchDB and implement the REST API based on CouchDB.
This article assumes that Git is already installed on your machine. If not, please refer to this article to install it.
Install node.js and npm
The easiest way is to go to the node.js official website through the nodejs download section page and select the installer under Mac, which will install Node.js and npm (node package manager).
After the installation is successful, you can use the node and npm commands.
Install CouchDB
Because this article requires CouchDB to store objects, CouchDB also needs to be installed.
Installing CouchDB is a little more troublesome, because we need to download the source code and then compile it. Before that, we need to install Homebrew. Please execute the following command:
git clone https://github.com/mxcl/homebrew.git cd homebrew/bin brew install autoconf automake libtool brew install couchdb
Important note: CouchDB previously reported a problem that may prevent you from installing it. To fix this problem, you need to manually edit the ~/couch/homebrew/Library/Formula/couchdb.rb file. The edit content is as follows:
class Couchdb url 'http://www.apache.org/dyn/closer.cgi?path=couchdb/source/1.1.1/apache-couchdb-1.1.1.tar.gz'
homepage "http://couchdb.apache.org/"
md5 'cd126219b9cb69a4c521abd6960807a6'
Please note that the source in the url needs to be deleted. The final modification result is as follows:
class Couchdb url 'http://www.apache.org/dyn/closer.cgi?path=couchdb/1.1.1/apache-couchdb-1.1.1.tar.gz'
homepage "http://couchdb.apache.org/"
md5 'cd126219b9cb69a4c521abd6960807a6'
If the installation process hangs, you need to CTRL-C to terminate and execute the following command to try again:
For more information about installing CouchDB on Mac OS X, please read "Installing CouchDB on OSX".
Once CouchDB is compiled, we can manually execute ./couchdb to start it. You can open the address http://127.0.0.1:5984/_utils in your browser to verify whether the CouchDB installation is successful.
Download Tutorial
Now that the required software has been installed, let’s continue with the Node.js introduction example.
First we use Git to obtain the instance source code
git clone https://github.com/indexzero/nodejs-intro.git
Create CouchDB database
Before starting the tutorial, we need to create a CouchDB database. First ensure that CouchDB has been started, and then use the following command to create the database:
$ curl -X PUT http://127.0.0.1:5984/pinpoint-dev10
{"ok":true}
You can visit http://127.0.0.1:5984/_utils in your browser to see the newly created database.
There is also a great guide to CouchDB here.
Start tutorial
The node js instance is built in a modular way. The lib directory contains many modules, and the server script is in the bin directory.
For example, if we want to start the CouchDB tutorial, we can execute the following command in the bin directory:
./server -t 02couchdb -s
The -t parameter allows you to specify the module in the lib directory to be executed, and the -s parameter is used to set the pinpoint-dev database we just created.
sys - util changes
Depending on the version of Node.js, you may see the following errors or warnings:
v0.7.7-pre
$ ./server -t 02couchdb -s
node.js:247
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: The "sys" module is now called "util".
at sys.js:1:69
at NativeModule.compile (node.js:572:5)
at Function.require (node.js:540:18)
at Function._load (module.js:297:25)
at Module.require (module.js:357:17)
at require (module.js:373:17)
at Object. (/home/ubuntu/nodejs-intro/bin/server:3:11)
at Module._compile (module.js:444:26)
at Object..js (module.js:462:10)
at Module.load (module.js:351:32)
To avoid this problem, you need to replace all calls `require("sys")` with `require("util")`
Node v0.6.14 will not throw an error message, but will prompt a warning:
v0.6.14
$ ./server -t 02couchdb -s
The "sys" module is now called "util". It should have a similar interface.
Pinpoint demo server listening for 02couchdb on http://127.0.0.1:8000
Run the tutorial
When you run a tutorial, some errors will be prompted:
The "sys" module is now called "util". It should have a similar interface.
node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
Error: Cannot find module 'optimist'
at Function._resolveFilename (module.js:332:11)
at Function._load (module.js:279:25)
at Module.require (module.js:354:17)
at require (module.js:370:17)
at Object. (/Users/ddewaele/Projects/Node/nodejs-intro/bin/server:5:12)
at Module._compile (module.js:441:26)
at Object..js (module.js:459:10)
at Module.load (module.js:348:31)
at Function._load (module.js:308:12)
at Array.0 (module.js:479:10)
该教程包含很多依赖,我们需要使用 npm 来下载这些依赖的包。
安装 node 包
Node packages (dependencies) 可通过 npm 命令来安装,例如:
$ npm install optimist npm http GET https://registry.npmjs.org/optimist npm http 200 https://registry.npmjs.org/optimist npm http GET https://registry.npmjs.org/optimist/-/optimist-0.2.8.tgz npm http 200 https://registry.npmjs.org/optimist/-/optimist-0.2.8.tgz npm http GET https://registry.npmjs.org/wordwrap npm http 200 https://registry.npmjs.org/wordwrap npm http GET https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz npm http 200 https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.2.tgz optimist@0.2.8 ../node_modules/optimist └── wordwrap@0.0.2
这些包将被安装到 node_modules 文件夹中:
$ ls -l ../node_modules/ total 0 drwxr-xr-x 10 ddewaele staff 340 Apr 1 18:54 optimist
本文需要安装如下的 node 包:
npm install winston npm install cradle npm install journey npm install optimist
运行教程
进入 bin 目录,通过下面命令来运行教程:
$ ./server -t 02couchdb -s The "sys" module is now called "util". It should have a similar interface. Pinpoint demo server listening for 02couchdb on http://127.0.0.1:8000
然后打开浏览器访问 http://127.0.0.1:8000/bookmarks ,将会看到如下的结果:
这表示服务已经启动并运行,为了在 CouchDB 中添加点测试数据,我们可以使用 http-console 控制台来访问 CouchDB 的 REST 服务。
安装 http-console
有一个非常棒的工具可以帮助你调试服务,该工具名为 http-console ,你可使用 npm 来安装:
sudo npm install -g http-console
然后就可以在命令行中执行该工具,不幸的是当我们执行该命令时报错了:
$ http-console node.js:201 throw e; // process.nextTick error, or 'error' event on first tick ^ Error: require.paths is removed. Use node_modules folders, or the NODE_PATH environment variable instead. at Function. (module.js:378:11) at Object. (/usr/local/lib/node_modules/http-console/bin/http-console:6:8) at Module._compile (module.js:441:26) at Object..js (module.js:459:10) at Module.load (module.js:348:31) at Function._load (module.js:308:12) at Array.0 (module.js:479:10) at EventEmitter._tickCallback (node.js:192:40)
很麻烦,我们还需要手工编辑 /usr/local/lib/node_modules/http-console/bin/http-console 文件,然后删除下面这一行:
现在 http-console 就可以启动了,无需任何参数,它将连接到 http://localhost:8080 ,如果你需要指定服务器和端口,把它作为第一个参数传递给 http-console 即可。
请注意我们这里使用了 \json 命令用来设置正确的 content-type:
$ http-console http://127.0.0.1:8000 The "sys" module is now called "util". It should have a similar interface. > http-console 0.6.1 > Welcome, enter .help if you're lost. > Connecting to 127.0.0.1 on port 8000. http://127.0.0.1:8000/> \json http://127.0.0.1:8000/>
访问 REST 服务
在 http-console 中,要执行 GET 请求只需要输入 GET /bookmarks 即可:
http://127.0.0.1:8000/> GET /bookmarks HTTP/1.1 200 OK Date: Sun, 01 Apr 2012 17:23:27 GMT Server: journey/0.4.0 Content-Type: application/json;charset=utf-8 Content-Length: 16 Connection: keep-alive { bookmarks: [] }
你也可以使用 JSON 的片段来执行 POST 请求:
http://127.0.0.1:8000/> POST /bookmarks ... { "url": "http://nodejs.org" } HTTP/1.1 200 OK Date: Thu, 05 Apr 2012 11:45:55 GMT Server: journey/0.4.0 Content-Type: application/json;charset=utf-8 Content-Length: 91 Connection: keep-alive { bookmark: { _id: 'WD-G-1', resource: 'Bookmark', url: 'http://nodejs.org' } }
然后再次执行 GET 请求,你就可以看到新插入的数据了:
http://127.0.0.1:8000/> GET /bookmarks HTTP/1.1 200 OK Date: Sun, 01 Apr 2012 17:23:27 GMT Server: journey/0.4.0 Content-Type: application/json;charset=utf-8 Content-Length: 16 Connection: keep-alive { bookmarks: [ { _rev: '1-cfced13a45a068e95daa04beff562360', _id: 'WD-G-1', resource: 'Bookmark', url: 'http://nodejs.org' } ] }

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Python is more suitable for data science and machine learning, while JavaScript is more suitable for front-end and full-stack development. 1. Python is known for its concise syntax and rich library ecosystem, and is suitable for data analysis and web development. 2. JavaScript is the core of front-end development. Node.js supports server-side programming and is suitable for full-stack development.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

WebStorm Mac version
Useful JavaScript development tools