-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
417 changed files
with
86,171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// create an express app | ||
|
||
var app = require('express')(), | ||
logger = require('morgan'); | ||
|
||
app.use(logger("dev")); | ||
|
||
// route handler for GET/ | ||
|
||
app.get('/', function(req, res) { | ||
res.send('<h1>hello world</h1>'); | ||
}); | ||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(3000); | ||
console.log("App is running on the server " + 3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* Simple Express routing example. | ||
* Can only access the following routes: | ||
* / | ||
* /about | ||
* /hello | ||
*/ | ||
|
||
var app = require('express')(), | ||
morgan = require('morgan'), | ||
port = process.env.PORT || 3000; | ||
|
||
|
||
// add logging middleware | ||
|
||
app.use(morgan('dev')); | ||
|
||
|
||
// route handler for GET / | ||
|
||
app.get('/', function(req, res) { | ||
res.send('Welcome to Express'); | ||
}); | ||
|
||
app.get('/about', function(req, res) { | ||
res.send('This just a simple Express ruting demo'); | ||
}); | ||
|
||
app.get('/hello', function(req, res) { | ||
res.send('Well, hello there!'); | ||
}); | ||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(port); | ||
console.log('server started on port %s', port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
*Simple Express routing example that | ||
*returns different content type responses. | ||
*Can only access the following routes: | ||
* | ||
* /hello1 | ||
* /hello2 | ||
* /hello3 | ||
* /hello4 | ||
* /hello5 | ||
* /hello6 | ||
* Ref: http://expressjs.com/4x/api.html#res.send | ||
*/ | ||
|
||
var app = require('express')(), | ||
morgan = require('morgan'), | ||
port = process.env.PORT || 3000; | ||
|
||
// add logging middleware | ||
|
||
app.use(morgan('dev')); | ||
|
||
// route handler for GET / | ||
|
||
app.get('/hello1', function(req, res) { | ||
res.send('<h1>hello world</h1>'); // automatic -> text/html | ||
}); | ||
|
||
app.get('/hello2', function(req, res) { | ||
res.header('Content-Type', 'text/plain'); | ||
res.send('hello world\n'); // explicit -> text/plain | ||
}); | ||
|
||
app.get('/hello3', function(req, res) { | ||
res.send(new Buffer('hello world\n')); // automatic -> application/octet-stream | ||
}); | ||
|
||
app.get('/hello4', function(req, res) { | ||
res.send({ | ||
message: 'hello world' | ||
}); // automatic -> application/json | ||
}); | ||
|
||
app.get('/hello5', function(req, res) { | ||
res.send(['hello world']); // automatic -> application/json | ||
}); | ||
|
||
app.get('/hello6', function(req, res) { | ||
res.header('Content-Type', 'text/xml'); | ||
res.send('<message>hello world</message>'); // explicit -> text/xml; | ||
}); | ||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(port); | ||
console.log('server started on port %s', port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
*Simple Express example for serving an API. | ||
*/ | ||
|
||
var app = require('express')(), | ||
bodyParser = require('body-parser'), | ||
morgan = require('morgan'), | ||
port = process.env.PORT || 3000, | ||
_ = require('lodash'), | ||
users = [{ | ||
id: 1, | ||
name: 'tony' | ||
}]; | ||
|
||
app.use(morgan('dev')); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
app.get('/users', function(req, res) { | ||
res.send({ | ||
success: true, | ||
users: users | ||
}); | ||
}); | ||
|
||
|
||
app.get('/users/:name', function(req, res) { | ||
var name = req.params.name; | ||
|
||
var user = _.find(users, function(u) { | ||
return u.name == name; | ||
}); | ||
|
||
var result = user ? { | ||
success: true, | ||
user: user | ||
} : { | ||
success: false, | ||
reason: 'user not found: ' + name | ||
}; | ||
|
||
res.send(result); | ||
}); | ||
|
||
|
||
app.post('/users', function(req, res) { | ||
var user = req.body; | ||
|
||
console.log(user); | ||
|
||
if (!user || !user.name) { | ||
res.send({ | ||
success: false, | ||
reason: 'cannot create user (missing user name)' | ||
}); | ||
return; | ||
} | ||
|
||
var existing = _.findWhere(users, { | ||
name: user.name | ||
}); | ||
|
||
if (existing) { | ||
res.send({ | ||
success: false, | ||
reason: 'user already exists: ' + existing.name | ||
}); | ||
return; | ||
} | ||
|
||
users.push(user); | ||
user.id = users.length; | ||
|
||
res.send({ | ||
success: true, | ||
user: user | ||
}); | ||
|
||
}); | ||
|
||
app.put('/users/:name', function(req, res) { | ||
var name = req.params.name, | ||
newName = req.body.name; | ||
|
||
var user = _.find(users, function(u) { | ||
return u.name == name; | ||
}); | ||
|
||
if (user) { | ||
user.name = newName; | ||
} | ||
|
||
var result = user ? { | ||
success: true, | ||
user: user | ||
} : { | ||
success: false, | ||
reason: 'user not found: ' + name | ||
}; | ||
|
||
res.send(result); | ||
}); | ||
|
||
|
||
app.delete('/users/:name', function(req, res) { | ||
var name = req.params.name; | ||
|
||
var user = _.find(users, function(u) { | ||
return u.name == name; | ||
}); | ||
|
||
var result = user ? { | ||
success: true, | ||
user: user | ||
} : { | ||
success: false, | ||
reason: 'user not found: ' + name | ||
}; | ||
|
||
|
||
users = _.reject(users, function(u) { | ||
return u.name == name; | ||
}); | ||
|
||
res.send(result); | ||
}); | ||
|
||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(port); | ||
console.log('server started on port %s', port); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
*Simple Express example for serving an API. | ||
*/ | ||
|
||
var app = require('express')(), | ||
bodyParser = require('body-parser'), | ||
morgan = require('morgan'), | ||
port = process.env.PORT || 3000, | ||
MongoClient = require('mongodb').MongoClient, | ||
mongoUrl = 'mongodb://localhost:27017/expressdemo', | ||
_db; | ||
|
||
app.use(morgan('dev')); | ||
|
||
app.use(bodyParser.json()); | ||
|
||
MongoClient.connect(mongoUrl, function(err, db) { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
console.log('connected to mongo'); | ||
_db = db; | ||
app.listen(port, function() { | ||
console.log('listening for requests on localhost:%s', port); | ||
}); | ||
} | ||
}); | ||
|
||
app.get('/users', function(req, res) { | ||
var collection = _db.collection('data'); | ||
collection.find({}).toArray(function(err, result) { | ||
if (err) { | ||
console.error(err); | ||
res.status(500).end(); | ||
} else { | ||
res.send({ | ||
success: true, | ||
users: result | ||
}); | ||
} | ||
}); | ||
}); | ||
|
||
|
||
app.get('/users/:name', function(req, res) { | ||
var name = req.params.name; | ||
var collection = _db.collection('data'); | ||
|
||
collection.findOne({ | ||
name: name | ||
}, function(err, user) { | ||
if (err) { | ||
console.error(err); | ||
res.status(500).end(); | ||
} else { | ||
var result = user ? { | ||
success: true, | ||
user: user | ||
} : { | ||
success: false, | ||
reason: 'user not found: ' + name | ||
}; | ||
res.send(result); | ||
} | ||
}); | ||
}); | ||
|
||
|
||
app.post('/users', function(req, res) { | ||
var user = req.body; | ||
var collection = _db.collection('data'); | ||
|
||
// validate user, make sure doesn't already exist, etc. | ||
|
||
collection.insert(user, function(err, users) { | ||
if (err) { | ||
console.error(err); | ||
res.status(500).end(); | ||
} else { | ||
res.send({ | ||
success: true, | ||
user: user | ||
}); | ||
} | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** Simple Node.js server | ||
*that responds with 'hello world' | ||
*/ | ||
|
||
var http = require('http'); | ||
|
||
function onListenEvent(req, res){ | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end('<h1>hello world</h1>'); | ||
} | ||
|
||
var app = http.createServer(onListenEvent); | ||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(3000, 'localhost'); | ||
console.log('server app running at localhost: 3000'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Simple Node.js http server | ||
*that reads index.html from the file system. | ||
*/ | ||
|
||
var http = require('http'), | ||
fs = require('fs'), | ||
path = require('path'); | ||
|
||
var app = http.createServer(function(req, res) { | ||
var index = path.join(__dirname, 'index.html'); | ||
if (req.url == '/' || req.url == '/index.html') { | ||
fs.readFile(index, function(err, data) { | ||
if (err) { | ||
console.error(err); | ||
res.writeHead(500, { | ||
'Content-Type': 'text/html' | ||
}); | ||
res.end('500 server error'); | ||
} else { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/html' | ||
}); | ||
res.end(data); | ||
} | ||
}); | ||
} else { | ||
console.log('resource not found: ' + req.url); | ||
res.writeHead(404, { | ||
'Content-Type': 'text/html' | ||
}); | ||
res.end('<html> <body> 404 not found </body>'); | ||
|
||
} | ||
}); | ||
|
||
//listen on localhost:3000 | ||
|
||
app.listen(3000, 'localhost'); | ||
console.log('server app running at localhost: 3000'); |
Oops, something went wrong.