有关Nodejs的用户管理应用

这篇博客详细介绍了如何使用MEAN(Mongodb+Express+Angularjs+Nodejs)框架构建一个用户管理应用。首先,需要安装Mongodb和Nodejs。接着,创建工程并利用Sublime Text管理文件,包括视图、控制器和路由。然后,通过npm安装Express和mongojs以处理数据库操作。在server.js中设置路由,并实现CRUD操作。在客户端,Angularjs用于处理交互。最后,通过运行Mongodb服务器和web服务,完成用户管理应用的部署。
摘要由CSDN通过智能技术生成

MEAN(Mongodb+Express+Angularjs+Nodejs)框架 

需要安装 Mongodb  Nodejs

创建一个工程 为了便于方便 我在这里使用了Sublime Text编辑器来管理文件 具体文件如下图


视图的创建  :/public/index.html

控制器的创建: /public/controller/index.html

构建路由 :/server.js

打开终端 在web1的路径 下面安装Express 安装指令为:

npm install express

安装Mongodb的API模块 mongojs 

npm install mongojs

server.js文件的代码如下

var express = require('express');
var app=express();
var mongojs=require('mongojs');
var db=mongojs('contactList',['contactList']);
var bodyParser=require('body-parser');
app.use(bodyParser.json());
app.use(express.static(__dirname +"/public"));
app.get('/contactList',function(req,res){
db.contactList.find(function(err,docs){
console.log(docs);
res.json(docs);
});
});
app.post('/contactList',function(req,res){
db.contactList.insert(req.body,function(err,doc){
res.json(doc);
});
});
app.delete('/contactList/:id',function(req,res){
var id =req.params.id;
console.log(id);
db.contactList.remove({_id:mongojs.ObjectId(id)},function(err,doc)
{
res.json(doc);
});
});
app.get('/contactList/:id',function(req,res){
var id =req.params.id;
db.contactList.findOne({_id:mongojs.ObjectId(id)},function(err,docs)
{
res.json(docs);
});
});
app.put('/contactList/:id',function(req,res){
var id=req.params.id;
db.contactList.findAndModify(
{
query:{_id:mongojs.ObjectId(id)},
update:{$set:{name:req.body.name,
email:req.body.email,
number:req.body.number}},
new:true
},function(err,doc){
res.json(doc);
});
});
app.listen(3000);


console.log("server running on port 3000");

controller.js内容:

var myApp=angular.module('myApp',[]);


myApp.controller('AppCtrl',['$scope','$http',function($scope,$http)
{

var refresh=function()
{
$http.get('/contactList').success(function(response)
{
console.log("I get the data from I request");
$scope.contactList=response;
$scope.contact='';
});
};
refresh();
$scope.addContact=function(){
console.log($scope.contact);
$http.post('/contactList',$scope.contact).success(function(response)
{
console.log(response);
refresh();
});
};
$scope.remove=function(id){
console.log(id);
$http.delete('/contactList/'+id).success(function(response)
{
refresh();
});
};
$scope.edit=function(id){
$http.get('/contactList/'+id).success(function(response)
{
$scope.contact=response;
});
};
$scope.update=function(){
$http.put('/contactList/'+$scope.contact._id,$scope.contact)
.success(function(response)
{
refresh();
});
};


}]);

index.html的内容:

<!DOCTYPE>
<!DOCTYPE html>
<html ng-app='myApp'>
<head>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js">
</script>
<script src="controllers/controller.js"></script>


<title>登陆表</title>
</head>
<body>
<div class="container" ng-controller="AppCtrl">
<h1>信息表</h1>
<table class="table">
<thead>
<th>用户名</th>
<th>Email</th>
<th>电话</th>
<th>操作</th>
</thead>

<tbody>
<tr ng-repeat="contact in contactList">
<td>{{contact.name}}</td>
<td>{{contact.email}}</td>
<td>{{contact.number}}</td>
<td>
<button class="btn btn-danger" ng-click="remove(contact._id)">
删除
</button>
</td>
<td>
<button class="btn btn-warning" ng-click="edit(contact._id)">
编辑
</button>
</td>
</tr>
<tr>
<td>
<input clsaa="form-control", ng-model="contact.name">
</td>
<td>
<input clsaa="form-control", ng-model="contact.email">
</td>
<td>
<input clsaa="form-control", ng-model="contact.number">
</td>
<td>
<button class="btn btn-primary" ng-click="addContact()">
添加
</button>
</td>
<td>
<button class="btn btn-info" ng-click="update()">
更新
</button>
</td>
</tr>
</tbody>
</table>
</div>
</body>

</html>

启动Mongodb的服务器 打开mongodb的bin目录下的mongod.exe

启动web服务  在终端窗口 输入:   node  server.js

如果得到下图结果 说明成功

接着打开浏览器 输入如下地址:

 http://localhost:3000

得到如下页面


对 数据库Mongodb的CRUD操作的单页面应用就 制作成功了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值