mirror of
https://github.com/wassname/talk.git
synced 2026-07-03 09:46:02 +08:00
Wrapped server routes into path based managment style
This commit is contained in:
+2
-4
@@ -10,8 +10,6 @@
|
||||
},
|
||||
"homepage": "https://github.com/jde/talk#readme",
|
||||
|
||||
"main": "src/server.js",
|
||||
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/jde/talk.git"
|
||||
@@ -24,8 +22,8 @@
|
||||
"test": "npm run -s lint && npm run -s test:karma",
|
||||
"test:karma": "karma start test/karma.conf.js --single-run",
|
||||
"lint": "eslint {src,test}",
|
||||
"server": "cd server; node index.js",
|
||||
"start": "cd server; node index.js"
|
||||
"server": "./server/bin/www",
|
||||
"start": "./server/bin/www"
|
||||
},
|
||||
|
||||
"keywords": [
|
||||
|
||||
@@ -9,6 +9,9 @@
|
||||
"error",
|
||||
2
|
||||
],
|
||||
"no-console": [
|
||||
0
|
||||
],
|
||||
"linebreak-style": [
|
||||
"error",
|
||||
"unix"
|
||||
@@ -1,16 +0,0 @@
|
||||
/* API routes contains general api funcitonality. */
|
||||
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
|
||||
router.use(function auth(req, res, next) {
|
||||
console.log('Read the user cookie here.');
|
||||
next();
|
||||
});
|
||||
|
||||
// Base route provides server version information.
|
||||
router.get('/', function(req, res) {
|
||||
res.send('App version information goes here....');
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,10 @@
|
||||
/* This is the entrypoint for the Talk Platform server. */
|
||||
|
||||
// Initialize application framework.
|
||||
const express = require('express');
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/api/v1', require('./routes/api'));
|
||||
|
||||
module.exports = app;
|
||||
Executable
+96
@@ -0,0 +1,96 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Setup the debug paramater.
|
||||
*/
|
||||
|
||||
process.env.DEBUG = process.env.TALK_DEBUG;
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
const app = require('../app');
|
||||
const debug = require('debug')('talk:server');
|
||||
const http = require('http');
|
||||
|
||||
/**
|
||||
* Get port from environment and store in Express.
|
||||
*/
|
||||
|
||||
const port = normalizePort(process.env.TALK_PORT || '3000');
|
||||
app.set('port', port);
|
||||
|
||||
/**
|
||||
* Create HTTP server.
|
||||
*/
|
||||
|
||||
const 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);
|
||||
}
|
||||
@@ -1,32 +0,0 @@
|
||||
/* Api comment routes define the handlers for incoming comment related requests. */
|
||||
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
|
||||
router.use(function (req, res, next) {
|
||||
console.log("Read the cookie to make sure users can do this thing with comments.")
|
||||
next();
|
||||
});
|
||||
|
||||
|
||||
router.get('/', function(req, res) {
|
||||
res.send('Read all of the comments ever');
|
||||
});
|
||||
|
||||
router.get('/:commentId', function(req, res) {
|
||||
res.send('Read a comment');
|
||||
});
|
||||
|
||||
router.post('/', function(req, res) {
|
||||
res.send('Write a comment');
|
||||
});
|
||||
|
||||
router.put('/:commentId', function(req, res) {
|
||||
res.send('Update a comment');
|
||||
});
|
||||
|
||||
router.delete('/:commentId', function(req, res) {
|
||||
res.send('Delete a comment');
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -1,46 +0,0 @@
|
||||
/* This is the entrypoint for the Talk Platform server. */
|
||||
|
||||
// Initialize application framework.
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
|
||||
// Load routes.
|
||||
var apiRoutes = require("./api/routes");
|
||||
var commentRoutes = require('./comments/routes');
|
||||
|
||||
// Initialize mongoose.
|
||||
var mongoose = require("mongoose");
|
||||
|
||||
// Connect to the database
|
||||
mongoose.connect('mongodb://localhost/talk');
|
||||
var db = mongoose.connection;
|
||||
db.on('error', console.error.bind(console, 'connection error:'));
|
||||
db.once('open', function() {
|
||||
ready();
|
||||
});
|
||||
|
||||
// Initialize Static Endpoints.
|
||||
var initializeStatic = function() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
// Initialize API endpoints.
|
||||
var initializeAPI = function () {
|
||||
app.use('/api/v1', apiRoutes);
|
||||
app.use('/api/v1/comments', commentRoutes);
|
||||
}
|
||||
|
||||
// Initialize server when ready.
|
||||
var ready = function () {
|
||||
|
||||
var port = 1618;
|
||||
|
||||
initializeStaticEndpoints();
|
||||
initializeAPI();
|
||||
|
||||
// Listen for incoming requests.
|
||||
app.listen(port, function () {
|
||||
console.log('Listening on port ' + port + '!');
|
||||
});
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
const mongoose = require('mongoose');
|
||||
const enabled = require('debug').enabled;
|
||||
|
||||
// Use native promises
|
||||
mongoose.Promise = global.Promise;
|
||||
|
||||
mongoose.connect(process.env.TALK_MONGO_URL);
|
||||
|
||||
if (enabled('talk:db')) {
|
||||
mongoose.set('debug', true);
|
||||
}
|
||||
|
||||
module.exports = mongoose;
|
||||
@@ -0,0 +1,25 @@
|
||||
const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
res.send('Read all of the comments ever');
|
||||
});
|
||||
|
||||
router.get('/:comment_id', (req, res) => {
|
||||
res.send('Read a comment');
|
||||
});
|
||||
|
||||
router.post('/', (req, res) => {
|
||||
res.send('Write a comment');
|
||||
});
|
||||
|
||||
router.put('/:comment_id', (req, res) => {
|
||||
res.send('Update a comment');
|
||||
});
|
||||
|
||||
router.delete('/:comment_id', (req, res) => {
|
||||
res.send('Delete a comment');
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,7 @@
|
||||
const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/comments', require('./comments'));
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,7 @@
|
||||
const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use('/api', require('./api'));
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user