NODEJS(5)Expressjs and CRUD
1. Play with Expressjs
>npm -version
1.4.9
>grunt -version
grunt-cli v0.1.11
Check the latest expressJS
>npm info express version
npmhttpGEThttps://registry.npmjs.org/expressnpmhttp304https://registry.npmjs.org/express 4.2.0
Then I put package.json there as follow>
{
"name": "hello",
"description": "first demo app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express" : "4.2.0"
}
}
Use command to install all the dependency
>npm install
And >npm ls will list all the dependencies.
And the simplest app.js should be as follow>
var express = require('express');
var app = express();
app.get('/hello.txt', function(req, res){
res.send('Hello Sillycat');
});
app.listen(3000);
console.log('Listening on port 3000');
And there a plenty of examples here
https://github.com/visionmedia/express/tree/master/examples
We can open the debug MODE
>DEBUG=express:* node app.js
https://github.com/visionmedia/debug
2. Using ExpressJS to build CRUD
>sudo npm install -g express
>sudo npm install -g express-generator
This will install some core Express functionality right into my Node installation.
Generate the Project
>express userlist
Or
>express —css stylus userlist
Go to that directory, >npm install will install all the dependencies.
Run the application will be as follow:
>DEBUG=userlist ./bin/www
Some key parts are as follow>
app.js Prepare the Monk Connection
// Mongodb Client Configuration
var mongo = require('mongodb’);
var monk = require('monk');
var db = monk('127.0.0.1:27010/userlist');
And Before we add the router, we need to expose the db client to the req
// make db available
app.use(function(req,res,next){
req.db = db;
next();
});
app.use('/', indexs);
app.use('/users', users);
Here is the service/mapping/control part, index.js which control all the router and http Method Mapping.
var express = require('express');
var debug = require('debug')('http');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express' });
});
/* GET users listing. */
router.get('/userlist', function(req, res) {
var db = req.db;
var users = db.get("users");
users.find({},{},function(e,docs){
res.render('userlist', {
"userlist" : docs
});
});
});
router.get('/newuser', function(req, res){
res.render('newuser', {title: 'Add New User' });
});
/* POST to Add User Service */
router.post('/adduser', function(req, res) {
var db = req.db;
// Get our form values. These rely on the "name" attributes
var userName = req.body.userName;
var userEmail = req.body.userEmail;
// Set our collection
var users = db.get('users');
// Submit to the DB
users.insert({
"userName" : userName,
"userEmail" : userEmail
}, function (err, doc) {
if (err) {
res.send("There was a problem adding the information to the database.");
}
else {
res.location("userlist");
res.redirect("userlist");
}
});
});
router.get('/deluser/:id', function(req, res){
debug("receive the id=" + req.params.id);
var db = req.db;
var id = req.params.id;
var users = db.get('users');
users.remove({_id: id}, function(err){
if(err){
res.send("Some Problem during deleting the user with id = " + id);
}
else {
res.location("/userlist");
res.redirect("/userlist");
}
})
});
module.exports = router;
And here is the jade part, I did not put much time on jade, actually my server side only provide REST API via JSON Format.
I plan to not use jade to construct my HTML.
userlist.jade is as follow>
extends layout
block content
h1.
User List
ul
each user, i in userlist
li
a(href="mailto:#{user.userEmail}")= user.userName
||||
a(href="/deluser/#{user._id}") Delete
the newuser.jade is as follow>
extends layout
block content
h1= title
form#formAddUser(name="adduser",method="post",action="/adduser")
input#inputUserName(type="text", placeholder="user name", name="userName")
input#inputUserEmail(type="text", placeholder="user email", name="userEmail")
button#btnSubmit(type="submit") submit
All the related codes and implementation are under easynodejs/userlist.
References:
http://cwbuecheler.com/web/tutorials/2013/node-express-mongo/
http://cwbuecheler.com/web/tutorials/2014/restful-web-app-node-express-mongodb/
https://github.com/cwbuecheler/node-tutorial-2-restful-app
http://www.vincent-durmont.com/2013/11/29/first-rest-api-with-node-express-monk-and-mongodb.html
http://expressjs.com/
http://sillycat.iteye.com/blog/2010679
Monk document
https://github.com/LearnBoost/monk
Deployment
forever
http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever/
https://github.com/nodejitsu/forever
http://stackoverflow.com/questions/18008620/node-js-express-js-app-only-works-on-port-3000
- 浏览: 2552566 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 574NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 371Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
带有TypeScript 3的Node.js Express API 具有TypeScript 3的Node.js Express API。支持MongoDB 描述 该生成器将帮助您使用TypeScript 3构建自己的Node.js Express Mongodb API。 项目介绍 suppot ES6 / ES7功能 ...
`routes`目录下的文件会处理与数据库相关的CRUD(创建、读取、更新、删除)操作。 5. **项目结构**: - `public`:存放静态资源,如CSS、JavaScript文件。 - `src`:React应用的主要代码存放地,包括组件、状态...
CRUD- Backbone.js NodeJS(ExpressJS)MongoDB(猫鼬ODM)前提: -节点-mongoDB 首先: -启动您的mongoDB -npm安装-npm开始http://本地主机:3000 /
5. **Mongoose**:Mongoose是用于Node.js的MongoDB对象模式工具,它简化了与MongoDB的交互,提供了ORM(对象关系映射)功能,帮助我们定义数据模型并处理数据验证、查询构建等任务。 6. **JSON Web Tokens (JWT)**...
创建读取更新删除(API) API CRUD dengan menggunakan NodeJS(ExpressJS)和MongoDB sebagai数据库NoSQL用于创建网站的工具必须安装的工具项目安装安装NPM npm install安装验证npm i fastest-validator安装DotEnv ...
Nodejs TODO列表API 此应用程序是使用NodeJS,ExpressJs和Mongoose构建的绝对多用户在线Todo管理REST API。 这是一个完整的CRUD应用程序,可帮助您管理待办事项列表。安装克隆存储库:git clone 在/server/config/...
后端:NodeJS,ExpressJS,MongoDB,Firebase,ExpoSDK 依存关系 "@bugsnag/expo": "^7.8.2", "@freakycoder/react-native-bounceable": "^0.2.4", "@react-native-async-storage/async-storage": "^1.14.1", "@...
待办事项列表服务器适用于使用NodeJS和ExpressJS构建并基于LinkedIn学习Shaun Wesell教程的待办事项列表应用程序的简单CRUD API。指示 git clone <this>cd todo-list-servernpm installnpm start
博客RESTful API 这是一个最小的博客API,具有使用NodeJS,ExpressJS和MongoDB开发的一些高级功能。 如果您正在寻找免费的RESTful API进行交互,请随时使用它。 它是开源的。产品特点通过JWT进行身份验证(通过电子...
标题 "shinichi:nodejs+expressjs+mongoose+mongodb" 暗示了一个使用Node.js、Express.js、Mongoose和MongoDB构建的项目。这是一个典型的JavaScript后端开发环境,通常用于快速开发Web应用程序。接下来,我们将深入...
nodejs-expressjs-mongodb-tutorial 只是一个很棒的 NODE.JS、EXPRESS、JADE 和 MONGODB 教程第 3 部分。 Christopher Buecheler 优秀教程的最后一步。 在这里,我使用 UPDATE 步骤完成了 CRUD 操作的最后一部分的...
在本项目中,MySQL将作为数据存储后端,与Node.js和Express.js配合,实现CRUD(创建、读取、更新和删除)操作。 **项目结构与工作流程** 1. **项目初始化**:首先,创建一个Node.js项目,安装Express.js和其他必要...
Node.js 身份验证和 CRUD API Node.js 身份验证和 CRUD API,使用 JWT、Passport.js 和 Sendgrid 进行电子邮件验证、图像上传和密码重置。 本分行电子邮件验证 其他分行 教程可获得教程。 测试 使用进行测试。 ...
ExpressJS 等等 特征 登录和注册 使用 JWT 进行身份验证和授权 创建、读取、更新和删除 (CRUD) 搜索、排序和分页 多张图片上传 产品交易/订单 允许的 CORS 以及许多即将制作/即将推出的功能 等等 先决条件 在开始...
5. **RESTful API设计**:理解REST原则,创建符合RESTful风格的URL,使用HTTP方法表示CRUD操作。 6. **错误处理**:在Node.js和Express.js中设置适当的错误处理机制,确保服务的健壮性。 7. **测试**:使用Mocha、...
节点表达均衡与Postgres或sqlite3兼容的CRUD API 参考:可选依赖 npm install -g nodemon指示 git clone <this>cd node-express-sequelizenpm installnode app.js or nodemon app.js# app should be running in ...
5. **中间件**: 在Express.js中,中间件是一种可以访问请求对象、响应对象和应用程序的请求处理链的函数。它们用于执行任何操作,如日志记录、验证、身份验证等。在hoalacxanhServer中,中间件可能用于处理错误、...
在Natours项目中,Express被用来定义路由,如创建、读取、更新和删除(CRUD)旅行信息,以及处理用户登录、注册等操作。 **MongoDB** MongoDB是一个流行的文档数据库,属于NoSQL数据库类型,适合存储非结构化或半...
使用Vuejs和Nodejs API进行CRUD 要求 Nodejs verion 8.9或更高版本 Mongodb在端口27017上本地运行 安装 将存储库克隆到本地计算机 转到项目目录 运行npm install命令 配置 配置基于我们运行应用程序的环境。 基本上...
为了继续,您需要以下内容: NodeJS和NPM ExpressJS-不需要视图模板MongoDB-连接可以是本地或远程猫鼬该项目假定您已安装ExpressJS,并且已成功连接到MongoDB。 我们将使用Mongoose提供的方法来查询数据库。 该项目...