diff --git a/package.json b/package.json index 2caacf24f..03f567d35 100644 --- a/package.json +++ b/package.json @@ -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": [ diff --git a/.eslintrc b/server/.eslintrc similarity index 89% rename from .eslintrc rename to server/.eslintrc index 201496cab..a28ebd512 100644 --- a/.eslintrc +++ b/server/.eslintrc @@ -9,6 +9,9 @@ "error", 2 ], + "no-console": [ + 0 + ], "linebreak-style": [ "error", "unix" diff --git a/server/api/routes.js b/server/api/routes.js deleted file mode 100644 index 2eb06fe1c..000000000 --- a/server/api/routes.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/server/app.js b/server/app.js new file mode 100644 index 000000000..e7ded2f9c --- /dev/null +++ b/server/app.js @@ -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; diff --git a/server/bin/www b/server/bin/www new file mode 100755 index 000000000..94408f5e8 --- /dev/null +++ b/server/bin/www @@ -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); +} diff --git a/server/comments/routes.js b/server/comments/routes.js deleted file mode 100644 index e7f389865..000000000 --- a/server/comments/routes.js +++ /dev/null @@ -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; \ No newline at end of file diff --git a/server/index.js b/server/index.js deleted file mode 100644 index 02b184ed0..000000000 --- a/server/index.js +++ /dev/null @@ -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 + '!'); - }); - -} \ No newline at end of file diff --git a/server/mongoose.js b/server/mongoose.js new file mode 100644 index 000000000..dcd361cc2 --- /dev/null +++ b/server/mongoose.js @@ -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; diff --git a/server/routes/api/comments/index.js b/server/routes/api/comments/index.js new file mode 100644 index 000000000..487550a8b --- /dev/null +++ b/server/routes/api/comments/index.js @@ -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; diff --git a/server/routes/api/index.js b/server/routes/api/index.js new file mode 100644 index 000000000..7390f2010 --- /dev/null +++ b/server/routes/api/index.js @@ -0,0 +1,7 @@ +const express = require('express'); + +const router = express.Router(); + +router.use('/comments', require('./comments')); + +module.exports = router; diff --git a/server/routes/index.js b/server/routes/index.js new file mode 100644 index 000000000..cf923e5dd --- /dev/null +++ b/server/routes/index.js @@ -0,0 +1,7 @@ +const express = require('express'); + +const router = express.Router(); + +router.use('/api', require('./api')); + +module.exports = router;