參考link
http://www.runoob.com/nodejs/nodejs-restful-api.html
// http://www.runoob.com/nodejs/nodejs-restful-api.html
// ////////////////////////////////////////////////////////////////////////////////////
// --Coding Standard
// 1 Enforce Callback Error Handling (handle-callback-err)
// https://eslint.org/docs/rules/handle-callback-err.html
//
// 2 Disallow string concatenation when using __dirname and __filename (no-path-concat)
// https://eslint.org/docs/rules/no-path-concat#disallow-string-concatenation-when-using-__dirname-and-__filename-no-path-concat
// Include All variables
// //////////////////////////////////////////////////////////////////////////////////////
var express = require('express')
var app = express()
var fs = require('fs')
var moment = require('moment')
var ROOT_PATH = __dirname
var JSON_FILE = 'users.json'
var FILE_ECODE_TYPE = 'utf8'
var fullJsonFilePath = ROOT_PATH + '/' + JSON_FILE
// Define All Basic Functions
// ///////////////////////////////////////////////////////////
function ListUserActionFunc (req, res) {
function GetFromJSONFunc (err, data) {
if (err) {
console.log(err.stack)
}
console.log('[%s]', moment().format('YYYY-MM-DD HH:mm:ss'))
console.log(data)
res.end(data)
}
fs.readFile(fullJsonFilePath, FILE_ECODE_TYPE, GetFromJSONFunc)
}
//
// ///////////////////////////////////////////////////////////
// 添加的新用户数据
var addUser = {
'user4': {
'name': 'mohit',
'password': 'password4',
'profession': 'teacher',
'id': 4
}
}
function AddUserActionFunc (req, res) {
function GetFromJSONFunc (err, data) {
if (err) {
console.log(err.stack)
}
data = JSON.parse(data)
data['user4'] = addUser['user4']
console.log('[%s]', moment().format('YYYY-MM-DD HH:mm:ss'))
console.log(data)
res.end(JSON.stringify(data))
}
// 读取已存在的数据
fs.readFile(fullJsonFilePath, FILE_ECODE_TYPE, GetFromJSONFunc)
}
function DeleteUserFunc (req, res) {
console.log('hello DeleteUserFunc')
function GetFromJSONFunc (err, data) {
if (err) {
console.log(err.stack)
}
var id = 2
console.log('hello GetFromJSONFunc')
data = JSON.parse(data)
delete data['user' + id]
console.log('[%s]', moment().format('YYYY-MM-DD HH:mm:ss'))
console.log(data)
res.end(JSON.stringify(data))
}
// First read existing users.
fs.readFile(fullJsonFilePath, FILE_ECODE_TYPE, GetFromJSONFunc)
}
//
// ///////////////////////////////////////////////////////////
function GetIdFunc (req, res) {
function GetFromJSONFunc (err, data) {
if (err) {
console.log(err.stack)
}
data = JSON.parse(data)
var user = data['user' + req.params.id]
console.log('[%s]', moment().format('YYYY-MM-DD HH:mm:ss'))
console.log(user)
res.end(JSON.stringify(user))
}
// 首先我们读取已存在的用户
fs.readFile(fullJsonFilePath, FILE_ECODE_TYPE, GetFromJSONFunc)
}
function SocketListenerFunc () {
var host = server.address().address
var port = server.address().port
console.log('Please visit http://%s:%s', host, port)
}
// Main Service Code
// //////////////////////////////////////////////////////////////////////////////////////
app.get('/listUsers', ListUserActionFunc)
app.get('/addUser', AddUserActionFunc)
app.get('/deleteUser', DeleteUserFunc)
app.get('/:id', GetIdFunc)
var server = app.listen(8081, SocketListenerFunc)
// Test URL
// http://127.0.0.1:8081/listUsers
// http://127.0.0.1:8081/addUser
// http://127.0.0.1:8081/2
// http://127.0.0.1:8081/deleteUser
分享到:
相关推荐
Node.js英文书籍2015出版共9本,打包成一个档,以节省大家的时间。 Full Stack Javascript - Learn...Pro REST API Development with Node.js (Apress 2015).pdf RESTful Web API Design with Node.js (Packt 2015).pdf
Pro REST API Development with Node.js shines light into that black hole of modules for the developers trying to create an API. Understand REST API development with Node.js using this book today. ...
Pro REST API Development with Node.js shines light into that black hole of modules for the developers trying to create an API. Understand REST API development with Node.js using this book today.
rest.js - 用于JavaScript的GitHub REST API客户端
该项目旨在使开发人员能够轻松地以最少的设置构建安全的REST API。 基础提供了定义具有或不具有CSRF保护的公共或私有路由的能力。
supersamples, node.js REST API的文档和示例 node.js rest式API的文档和示例 supersamples 是一个支持 Supertest插件,能够生成可靠且易于使用的API示例的 mocha 插件报告程序。 简而言之:在测试套件中定义具体
使用node.js技术开发RestFul API
PGRestAPI, 用于PostGres空间实体的node.js REST API AKA PGRestAPI ( 一个。k 。Chubbs空间服务器) 概述用于PostgreSQL空间表的Node.js REST API 。PGRestAPI的几个关键用法:从PostGIS或者shapefile快速创建 M
Pro REST API Development with Node.js shines light into that black hole of modules for the developers trying to create an API. Understand REST API development with Node.js using this book today. ...
Node.js(REST API)+ Vue.js / Nuxt.js(前端/后端)+ MySQL样板 这是一个样板项目。 该项目包含Node.js REST API和Vue.js使用BootstrapVue开发的前端/后端。 API Node.js,Express,Webpack,Express Validator,...
Chapter 8: Building Node.js REST API Servers with Express.js and Hapi Chapter 9: Real-Time Apps with WebSocket, Socket.IO, and DerbyJS Chapter 10: Getting Node.js Apps Production Ready Chapter 11: ...
Node.js and its modules can help create a RESTful API. You’ll also get a taste of what a real-world development flow would be like and what it would take to both test and deploy your code into a ...
node-neo4j, node.js的Neo4j 图形数据库驱动程序( REST API 客户端) Node-Neo4j这是访问 Neo4j的客户端库,它是一个图形数据库,来自 node.js 。 它使用 Neo4j REST API的,并支持 Neo4j 1.5通过 Neo4j 2.1.现在,像...
简单的node.js CRUD Rest Api 在你开始之前 克隆项目 git clone https://github.com/michnaadam33/note-crud-api.git 安装依赖项 cd npm install 移民 执行迁移 cd ./node_modules/sequelize-cli/bin/sequelize db...
nforce :: node.js salesforce REST API包装器 nforce是node.js,是force.com,database.com和salesforce.com的REST API包装器。 特征 简单的API 智能对象 辅助OAuth方法 简单的流媒体 单用户模式下的多用户设计 ...
ChatGPT和Bing AI的客户端实现。可用作Node.js模块、REST API服务器和CLI应用.zip
一个REST API,用于使用Node.js,headless Chrome和Cheerio来爬取动态网站
“#Express-Sequelize-MSSQL-with-Node.js-Rest-API-” Express是Node.js最受欢迎的Web框架之一,支持路由,中间件和显示系统。 Sequelize是用于Postgres,MySQL,SQL Server的简化的基于单词的Node.js ORM。 在此...
使用不带框架的Node.js的完整CRUD REST API 这是出于学习/实验目的。在大多数情况下,您会在生产项目中使用Express之类的东西 # Routes GET /api/products POST /api/products GET /api/products/:id PUT /api/...