Skip to content

Commit

Permalink
寒假在网吧写了雏形,接下来慢慢填坑
Browse files Browse the repository at this point in the history
  • Loading branch information
shuiRong committed Feb 13, 2017
0 parents commit ea09358
Show file tree
Hide file tree
Showing 74 changed files with 20,493 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
80 changes: 80 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
/*----------------------------------------------------------------------------*/
/*var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');*/
/*----------------------------------------------------------------------------*/

var index = require('./routes/index');
var users = require('./routes/users');
var indexApi = require('./routes/index-api');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());

app.use(express.static(path.join(__dirname,('public'))));
app.use(express.static(path.join(__dirname,('routes'))));

app.use('/', index);
app.use('/users', users);
app.use('/api',indexApi);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

/*----------------------------------------------------------------------------*/

/*MongoClient.connect('mongodb://127.0.0.1:27017/test2',function(err,db){
/*assert.equal(null,err);*/
/* if(err){
throw err;
}
console.log('Connected successfully---------------');
insertDoc(db,function(){
db.close();
})
*/
/*db.collection('restaurants').find({'grades.score':{$gt:95}}).toArray(function(err,result){
if(err){
throw err;
}
console.log(result);
})*/
//})

//function insertDoc()


/*----------------------------------------------------------------------------*/

module.exports = app;
25 changes: 25 additions & 0 deletions bin/npm-debug.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
0 info it worked if it ends with ok
1 verbose cli [ 'E:\\nodejs\\node.exe',
1 verbose cli 'C:\\Users\\windows\\AppData\\Roaming\\npm\\node_modules\\cnpm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli '--userconfig=C:\\Users\\windows\\.cnpmrc',
1 verbose cli '--disturl=https://npm.taobao.org/mirrors/node',
1 verbose cli '--registry=https://registry.npm.taobao.org',
1 verbose cli 'start' ]
2 info using npm@3.10.10
3 info using node@v6.9.4
4 verbose stack Error: ENOENT: no such file or directory, open 'E:\code\myapp1\bin\package.json'
4 verbose stack at Error (native)
5 verbose cwd E:\code\myapp1\bin
6 error Windows_NT 6.1.7601
7 error argv "E:\\nodejs\\node.exe" "C:\\Users\\windows\\AppData\\Roaming\\npm\\node_modules\\cnpm\\node_modules\\npm\\bin\\npm-cli.js" "--userconfig=C:\\Users\\windows\\.cnpmrc" "--disturl=https://npm.taobao.org/mirrors/node" "--registry=https://registry.npm.taobao.org" "start"
8 error node v6.9.4
9 error npm v3.10.10
10 error path E:\code\myapp1\bin\package.json
11 error code ENOENT
12 error errno -4058
13 error syscall open
14 error enoent ENOENT: no such file or directory, open 'E:\code\myapp1\bin\package.json'
15 error enoent ENOENT: no such file or directory, open 'E:\code\myapp1\bin\package.json'
15 error enoent This is most likely not a problem with npm itself
15 error enoent and is related to npm not being able to find a file.
16 verbose exit [ -4058, true ]
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('myapp1:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
17 changes: 17 additions & 0 deletions database/conn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var MongoClient = require('mongodb').MongoClient;

MongoClient.connect('mongodb://127.0.0.1:27017/test',function(err,db){
if(err){
throw err;
}

db.collection('restaurants').find({'grades.score':{$gt:95}}).toArray(function(err,result){
if(err){
throw err;
}

console.log(result);
})
})

module.exports = MongoClient;
76 changes: 76 additions & 0 deletions database/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var mongodb = require('mongodb');
var server = new mongodb.Server('localhost',27017,{auto_reconnect:true});
var db = new mongodb.Db('local',server);
/*
db.open(function(err,db){
if(!err){
console.log('connect db --ing');
db.collection('content',function(err,collection){
if(err){
console.log(err);
}else{
var temp1 = {id:'1',title:'hello',number:1};
var temp2 = {id:'2',title:'world',number:2};
collection.insert([temp1,temp2],function(err,result){
if(err){
throw err;
}
console.log('result----------------');
console.log(result);
console.log('++++++++++++++++++++++');
});
collection.find().toArray(function(err,docs){
if(err){
throw err;
}
console.log('find---------------');
console.log(docs);
console.log('++++++++++++++++++++++');
});
collection.update({title:'hello'}, {$set:{number:5}},{multi:true},function(err,result){
});
collection.find().toArray(function(err,docs){
if(err){
throw err;
}
console.log('find2--------------');
console.log(docs);
console.log('++++++++++++++++++++++');
});
}
});
}else{
console.log(err);
}
})
*/

function insertFunc(jsonData){
db.open(function(err,db){
if(!err){
console.log('connectting database !');
db.collection('content',function(err,collection){
if(err){
console.log(err);
}else{
collection.insert(jsonData,function(err,result){
if(err){
throw err;
}
console.log('insert jsonData into collection! DONE!');
});
}
})
}else{
console.log(err);
}
})
}

module.exports = insertFunc;
Loading

0 comments on commit ea09358

Please sign in to comment.