Created
July 17, 2016 16:03
-
-
Save unisys12/597c01e1a009d5bdd0ea9d2c598eb2ce to your computer and use it in GitHub Desktop.
Nodejs POST Request Error
This file contains hidden or 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
# bot.js not included here. starts to whole process. Just shows process has started. | |
Logging from bot.js | |
# Data sample grabbed from line 23 of logs.js | |
{"_id":"204255289342427136","server":"FFC_Test_Server","channel":"general","author":"Unisys1 | |
2","author_id":"154928746636378112","message":"test message to MongoDB Server using Mongoose | |
","mentions":[],"timestamp":1468768655859} | |
# Line 52 of logs.js | |
Writing the data to the request | |
# Line 55 of logs.js | |
Ending request life cycle | |
# Line 48 of logs.js | |
problem with request: socket hang up |
This file contains hidden or 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
var http = require('http'); | |
var querystring = require('querystring'); | |
/** | |
* Conlose.log outputs are noted in | |
* console_output.bash included below | |
* */ | |
module.exports = { | |
logMessage: function(data) { | |
var data = JSON.stringify({ | |
'_id': data.id, | |
'server': data.server.name, | |
'channel': data.channel.name, | |
'author': data.author.username, | |
'author_id': data.author.id, | |
'message': data.cleanContent, | |
'mentions': data.mentions, | |
'timestamp': data.timestamp, | |
}); | |
console.log(data); | |
var options = { | |
host: 'localhost', | |
port: 8080, | |
path: 'ffcbot/logs', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(data) | |
} | |
}; | |
var req = http.request(options, function(res) { | |
console.log("Preparing the request to " + options.path); | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
console.log('body:' + chunk); | |
}); | |
res.on('end', function(){ | |
console.log('No more Data!'); | |
}); | |
}); | |
req.on('error', function(e){ | |
console.log(`problem with request: ${e.message}`); | |
}); | |
console.log("Writing the data to the request"); | |
req.write(data); | |
console.log("Ending request life cycle"); | |
req.end(); | |
} | |
} |
This file contains hidden or 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
// server.js | |
// BASE SETUP | |
// ============================================================================= | |
// call the packages we need | |
var express = require('express'); // call express | |
var app = express(); // define our app using express | |
var bodyParser = require('body-parser'); | |
// Configure Database Connections | |
//var db = require('./bot_core/database.js'); | |
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost:27017/local'); | |
var Log = require('./bot_core/Models/logs.js'); | |
// configure app to use bodyParser() | |
// this will let us get the data from a POST | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.use(bodyParser.json()); | |
var port = process.env.PORT || 8080; // set our port | |
// ROUTES FOR OUR API | |
// ============================================================================= | |
var router = express.Router(); // get an instance of the express Router | |
// middleware to use for all requests | |
router.use(function(error, req, res, next) { | |
if (!error) { | |
console.log('Something is happening.'); | |
next(); // make sure we go to the next routes and don't stop here | |
} else { | |
console.error(error.stack); | |
res.send(500); | |
} | |
}); | |
// test route to make sure everything is working (accessed at GET http://localhost:8080/api) | |
router.get('/', function(req, res) { | |
res.json({ message: 'This will be the Bot Home Page' }); | |
}); | |
router.route('/logs') | |
// POST Function | |
.post(function(req, res) { | |
console.log("Post Route Hit"); | |
var log = new Log(); | |
log._id = req.body._id; | |
log.server = req.body.server; | |
log.channel = req.body.channel; | |
log.author = req.body.author; | |
log.author_id = req.body.author_id; | |
log.mentions = req.body.mentions; | |
log.save(function (err) { | |
if (err) | |
res.send(err); | |
res.json({message: 'New Log Entry Saved!'}); | |
}); | |
}) | |
// GET all Logs | |
.get(function(req, res) { | |
console.log('Get Logs has been called!'); | |
var query = Log.find({}); | |
query.select('server channel message'); | |
query.exec(function (err, chatlogs) { | |
if (err) {return handleError(err);} | |
res.json(chatlogs); | |
}) | |
}); | |
// more routes for our API will happen here | |
// REGISTER OUR ROUTES ------------------------------- | |
// all of our routes will be prefixed with /api | |
app.use('/ffcbot', router); | |
// START THE SERVER | |
// ============================================================================= | |
app.listen(port); | |
console.log('Magic happens on port ' + port); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment