node.js - HTTP requests makes server hang -
i trying build rest api in expressjs, far i've had lot of trouble generating json responses. if send new record post requests 1 of endpoints, gets inserted database, server keeps hanging , doesnt respond. likewise, if make request 1 of resources, nothing happens, server hangs again.
routes file:
'use strict' calendar = require './models/calendar' module.exports = (app, express) -> router = express.router() # log http requests console router.use (req, res, next) -> console.log req.method, req.url next() # calendar routes router.get '/calendars', (req, res) -> calendar.find (err, calendars) -> if err res.send(err) res.json(calendars) router.post '/calendars', (req, res) -> calendar = new calendar calendar.name = req.body.name calendar.save (err, calendar) -> if err next(err) res.json(200, calendar) # namespace api app.use('/api', router)
server file:
'use strict'; express = require 'express' path = require 'path' fs = require 'fs' # set default node environment development process.env.node_env = process.env.node_env || 'development'; config = require './server/config/config' app = express() require('./server/config/express') app require('./server/routes') app, express # start server app.listen config.port, config.ip, -> console.log 'express server listening on %s:%d, in %s mode', config.ip, config.port, app.get 'env' # expose app exports = module.exports = app;
config file:
path = require 'path' express = require 'express' favicon = require 'static-favicon' cookieparser = require 'cookie-parser' errorhandler = require 'errorhandler' session = require 'express-session' config = require './config' mongostore = require('connect-mongo') session bodyparser = require 'body-parser' module.exports = (app) -> env = app.get 'env' if env 'development' app .use require('connect-livereload')() .use errorhandler() #disables caching of scripts in development module #todo: paths? .use (req, res, next) -> res.header 'cache-control', 'no-cache, no-store, must-revalidate' res.header 'pragma', 'no-cache' res.header 'expires', 0 next() sessionstore = new mongostore url: config.mongo.uri collection: 'sessions' app .use express.static path.join config.root, '.tmp' .use express.static path.join config.root, 'client' .use bodyparser.json() .use bodyparser.urlencoded(extended: true) .set 'views', "#{config.root}/client" .set 'view engine', 'jade' .use cookieparser() .use session secret: 'my-little-secret' store: sessionstore
Comments
Post a Comment