<commit_before>// 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');
var mongoose	= require('mongoose');
var Bear	= require('./app/models/bear');

// configure the app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());

// connect to the db
mongoose.connect('mongodb://localhost:27017/mydb');

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(req, res, next) {
	// do logging
	console.log('Something is happening');
	next(); // make sure we go to the next routes and don't stop here
});

// test route to make sure everyting is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
	res.json({ message: 'hooray! welcome to our api!' });
});

// more routes for our API will happen here

// on routes that end in /bears
// ---------------------------------------------
router.route('/bears')

	// create a bear (accessed at POST http://localhost:8080/api/bears)
	.post(function(req, res) {
		var bear = new Bear();		// create a new instance of the Bear model
		bear.name = req.body.name;	// set the bear's name (comes form the request)
		// save the bear and check for errors
		bear.save(function(err) {
			if (err) {
				res.send(err);
			}
			res.json({ message: 'Bear created!' });
		});
	})

	// get all the bears (accessed at GET http://localhost:8080/api/bears)
	.get(function(req, res) {
		Bear.find(function(err, bears) {
			if (err) {
				res.send(err);
			}
			res.json(bears);
		});
	});

// on routes that end in /bears/:beard_id
// ----------------------------------------------
router.route('/bears/:bear_id')

	// get the bear with that id (accessed at GET http://localhost:8080/api/bears/:bear_id)
	.get(function(req, res) {
		Bear.findById(req.params.bear_id, function(err, bear) {
			if (err) {
				res.send(err);
			}
			res.json(bear);
		});
	});


// REGISTER OUR ROUTES --------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);


// START THE SERVER
// =========================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

<commit_msg>added put (update) by id<commit_after>// 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');
var mongoose	= require('mongoose');
var Bear	= require('./app/models/bear');

// configure the app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true}));
app.use(bodyParser.json());

// connect to the db
mongoose.connect('mongodb://localhost:27017/mydb');

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(req, res, next) {
	// do logging
	console.log('Something is happening');
	next(); // make sure we go to the next routes and don't stop here
});

// test route to make sure everyting is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
	res.json({ message: 'hooray! welcome to our api!' });
});

// more routes for our API will happen here

// on routes that end in /bears
// ---------------------------------------------
router.route('/bears')

	// create a bear (accessed at POST http://localhost:8080/api/bears)
	.post(function(req, res) {
		var bear = new Bear();		// create a new instance of the Bear model
		bear.name = req.body.name;	// set the bear's name (comes form the request)
		// save the bear and check for errors
		bear.save(function(err) {
			if (err) {
				res.send(err);
			}
			res.json({ message: 'Bear created!' });
		});
	})

	// get all the bears (accessed at GET http://localhost:8080/api/bears)
	.get(function(req, res) {
		Bear.find(function(err, bears) {
			if (err) {
				res.send(err);
			}
			res.json(bears);
		});
	});

// on routes that end in /bears/:beard_id
// ----------------------------------------------
router.route('/bears/:bear_id')

	// get the bear with that id (accessed at GET http://localhost:8080/api/bears/:bear_id)
	.get(function(req, res) {
		Bear.findById(req.params.bear_id, function(err, bear) {
			if (err) {
				res.send(err);
			}
			res.json(bear);
		});
	})

	// update the bear with this id (accessed at PUT http://localhost:8080/api/bears/:bear_id)
	.put(function(req, res) {
		// use our bear model to find the bear we want
		Bear.findById(req.params.bear_id, function(err, bear) {
			if (err) {
				res.send(err);
			}
			bear.name = req.body.name;	// update the bear's name

			// save the bear
			bear.save(function(err) {
				if (err) {
					res.send(err);
				}
				res.json({ message: 'Bear updated!' });
			});
		});
	});


// REGISTER OUR ROUTES --------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);


// START THE SERVER
// =========================================================================
app.listen(port);
console.log('Magic happens on port ' + port);


{'fraction_non_alphanumeric': 0.25542168674698795, 'fraction_numerical': 0.00929432013769363, 'mean_word_length': 2.915768194070081, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '849194', 'n_tokens_mistral': 1951, 'n_tokens_neox': 1759, 'n_words': 553}