├── README.md ├── views ├── index.pug ├── error.pug └── layout.pug ├── .gitignore ├── public ├── stylesheets │ └── style.css ├── controllers │ └── main.controller.js └── index.html ├── routes ├── users.js └── index.js ├── models └── user.js ├── package.json ├── bin └── www └── app.js /README.md: -------------------------------------------------------------------------------- 1 | # MEAN_CRUD 2 | This is a basic CRUD application using MEAN. 3 | -------------------------------------------------------------------------------- /views/index.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= title 5 | p Welcome to #{title} 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .tmp 3 | .sass-cache 4 | .idea 5 | dist 6 | npm-debug.log 7 | **/.DS_Store 8 | .vscode 9 | -------------------------------------------------------------------------------- /views/error.pug: -------------------------------------------------------------------------------- 1 | extends layout 2 | 3 | block content 4 | h1= message 5 | h2= error.status 6 | pre #{error.stack} 7 | -------------------------------------------------------------------------------- /public/stylesheets/style.css: -------------------------------------------------------------------------------- 1 | body { 2 | padding: 50px; 3 | font: 14px "Lucida Grande", Helvetica, Arial, sans-serif; 4 | } 5 | 6 | a { 7 | color: #00B7FF; 8 | } 9 | -------------------------------------------------------------------------------- /views/layout.pug: -------------------------------------------------------------------------------- 1 | doctype html 2 | html 3 | head 4 | title= title 5 | link(rel='stylesheet', href='/stylesheets/style.css') 6 | body 7 | block content 8 | -------------------------------------------------------------------------------- /routes/users.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET users listing. */ 5 | router.get('/', function(req, res, next) { 6 | res.send('respond with a resource'); 7 | }); 8 | 9 | module.exports = router; 10 | -------------------------------------------------------------------------------- /models/user.js: -------------------------------------------------------------------------------- 1 | var mongoose = require('mongoose'); 2 | var schema = mongoose.Schema; 3 | 4 | userShema = new schema({ 5 | name: {type: String, require: true}, 6 | email: {type: String, require: true} 7 | }) 8 | 9 | module.exports = mongoose.model("User", userShema); 10 | -------------------------------------------------------------------------------- /routes/index.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var router = express.Router(); 3 | 4 | /* GET home page. */ 5 | router.get('/', function(req, res, next) { 6 | res.render('index', { title: 'Express' }); 7 | }); 8 | 9 | router.get('/fuck', function(req, res, next) { 10 | res.send('my first resopond'); 11 | }); 12 | 13 | module.exports = router; 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "mean-crud", 3 | "version": "0.0.0", 4 | "private": true, 5 | "scripts": { 6 | "start": "node ./bin/www" 7 | }, 8 | "dependencies": { 9 | "body-parser": "~1.16.0", 10 | "cookie-parser": "~1.4.3", 11 | "debug": "~2.6.0", 12 | "express": "~4.14.1", 13 | "mongoose": "^4.9.0", 14 | "morgan": "~1.7.0", 15 | "pug": "~2.0.0-beta10", 16 | "serve-favicon": "~2.3.2" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /public/controllers/main.controller.js: -------------------------------------------------------------------------------- 1 | var app = angular.module('myfirstmean', []); 2 | 3 | app.controller('addcontrolle', function ($scope, $http) { 4 | 5 | var refresh = function () { 6 | $http.get('/contactlists').then(function (response) { 7 | $scope.contactlist = response.data; 8 | $scope.contact.name = ''; 9 | $scope.contact.email = ''; 10 | }); 11 | }; 12 | 13 | refresh(); 14 | // 15 | $scope.addData = function () { 16 | $http.post("/contactlists", $scope.contact).then(function (response) { 17 | console.log(response); 18 | refresh(); 19 | }) 20 | }; 21 | 22 | $scope.remove = function (id) { 23 | $http.delete('/contactlists/' + id).then(function (response) { 24 | refresh(); 25 | }) 26 | }; 27 | 28 | $scope.edit = function () { 29 | $http.put('/contactlists/' + $scope.contact._id, $scope.contact).then(function (response) { 30 | refresh(); 31 | }); 32 | }; 33 | 34 | $scope.update = function (id) { 35 | $http.get('/contact/' + id).then(function (response) { 36 | $scope.contact = response.data; 37 | }); 38 | }; 39 | }); 40 | -------------------------------------------------------------------------------- /bin/www: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | /** 4 | * Module dependencies. 5 | */ 6 | 7 | var app = require('../app'); 8 | var debug = require('debug')('myapp:server'); 9 | var http = require('http'); 10 | 11 | /** 12 | * Get port from environment and store in Express. 13 | */ 14 | 15 | var port = normalizePort(process.env.PORT || '3000'); 16 | app.set('port', port); 17 | 18 | /** 19 | * Create HTTP server. 20 | */ 21 | 22 | var server = http.createServer(app); 23 | 24 | /** 25 | * Listen on provided port, on all network interfaces. 26 | */ 27 | 28 | server.listen(port); 29 | server.on('error', onError); 30 | server.on('listening', onListening); 31 | 32 | /** 33 | * Normalize a port into a number, string, or false. 34 | */ 35 | 36 | function normalizePort(val) { 37 | var port = parseInt(val, 10); 38 | 39 | if (isNaN(port)) { 40 | // named pipe 41 | return val; 42 | } 43 | 44 | if (port >= 0) { 45 | // port number 46 | return port; 47 | } 48 | 49 | return false; 50 | } 51 | 52 | /** 53 | * Event listener for HTTP server "error" event. 54 | */ 55 | 56 | function onError(error) { 57 | if (error.syscall !== 'listen') { 58 | throw error; 59 | } 60 | 61 | var bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port; 62 | 63 | // handle specific listen errors with friendly messages 64 | switch (error.code) { 65 | case 'EACCES': 66 | console.error(bind + ' requires elevated privileges'); 67 | process.exit(1); 68 | break; 69 | case 'EADDRINUSE': 70 | console.error(bind + ' is already in use'); 71 | process.exit(1); 72 | break; 73 | default: 74 | throw error; 75 | } 76 | } 77 | 78 | /** 79 | * Event listener for HTTP server "listening" event. 80 | */ 81 | 82 | function onListening() { 83 | var addr = server.address(); 84 | var bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; 85 | debug('Listening on ' + bind); 86 | } 87 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var express = require('express'); 2 | var path = require('path'); 3 | var favicon = require('serve-favicon'); 4 | var logger = require('morgan'); 5 | var cookieParser = require('cookie-parser'); 6 | var bodyParser = require('body-parser'); 7 | 8 | var index = require('./routes/index'); 9 | var users = require('./routes/users'); 10 | 11 | var User = require('./models/user'); 12 | 13 | var app = express(); 14 | var mongoose = require('mongoose'); 15 | 16 | //connection og MongoDB 17 | var conn = mongoose.connect('localhost:27017/myapp'); 18 | 19 | if (conn) { 20 | console.log('MongoDB Connected'); 21 | } else { 22 | console.log('MongoDB NOT Connected'); 23 | } 24 | 25 | // view engine setup 26 | app.set('views', path.join(__dirname, 'views')); 27 | app.set('view engine', 'pug'); 28 | 29 | // uncomment after placing your favicon in /public 30 | 31 | 32 | //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 33 | app.use(logger('dev')); 34 | app.use(bodyParser.json()); 35 | app.use(bodyParser.urlencoded({ extended: false })); 36 | app.use(cookieParser()); 37 | app.use(express.static(path.join(__dirname, 'public'))); 38 | 39 | app.use('/', index); 40 | app.use('/users', users); 41 | 42 | //view requests 43 | app.post('/contactlists', function(req,res) { 44 | console.log(">>>> " + req.body); 45 | var userTable = new User(req.body); 46 | userTable.save(function(err, docs) { 47 | console.log(docs); 48 | res.json(docs); 49 | }) 50 | }); 51 | 52 | app.get('/contactlists', function(req, res) { 53 | User.find(function(err,docs){ 54 | //console.log(docs); 55 | res.json(docs) 56 | }) 57 | }); 58 | 59 | app.delete('/contactlists/:id', function(req, res) { 60 | var id = req.params.id; 61 | //console.log(id); 62 | User.remove({ _id: id }, function(err, doc) { 63 | res.json(doc); 64 | }) 65 | }) 66 | 67 | app.get('/contact/:id', function(req, res) { 68 | var id = req.params.id; 69 | User.findOne({_id:id}, function(err, doc) { 70 | res.json(doc); 71 | }); 72 | }); 73 | 74 | app.put('/contactlists/:id', function(req, res) { 75 | var id = req.params.id; 76 | //console.log(req.body.name); 77 | User.findOneAndUpdate({_id: id}, {$set: {name: req.body.name, email: req.body.email}}, {new: true}, function(err, doc) { 78 | res.json(doc); 79 | }); 80 | }); 81 | 82 | 83 | // catch 404 and forward to error handler 84 | app.use(function(req, res, next) { 85 | var err = new Error('Not Found'); 86 | err.status = 404; 87 | next(err); 88 | }); 89 | 90 | // error handler 91 | app.use(function(err, req, res, next) { 92 | // set locals, only providing error in development 93 | res.locals.message = err.message; 94 | res.locals.error = req.app.get('env') === 'development' ? err : {}; 95 | 96 | // render the error page 97 | res.status(err.status || 500); 98 | res.render('error'); 99 | }); 100 | 101 | module.exports = app; 102 | -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 || Name | 20 |Actions | 22 |||
|---|---|---|---|
|
27 |
28 |
29 |
30 | |
31 |
32 |
33 |
34 |
35 | |
36 |
37 | |
41 |
42 | |
46 |
| {{contact.name}} | 49 |{{contact.email}} | 50 |
51 | |
55 |
56 | |
60 |