Tell us what’s happening:
Hi all,
I’m working on URL Shortener Microservice.
In the first step I tried to save something that send to the server via POST method in the database with this code
Your code so far
'use strict';
var express = require('express');
var mongo = require('mongodb');
var mongoose = require('mongoose');
var cors = require('cors');
var app = express();
// Basic Configuration
var port = process.env.PORT || 3000;
/** this project needs a db !! **/
// mongoose.connect(process.env.MONGOLAB_URI);
app.use(cors());
/** this project needs to parse POST bodies **/
// you should mount the body-parser here
var bodyParser = require('body-parser');
app.use('/public', express.static(process.cwd() + '/public'));
app.use(bodyParser.urlencoded({extended: false}));
app.get('/', function(req, res){
res.sendFile(process.cwd() + '/views/index.html');
});
// your first API endpoint...
app.get("/api/hello", function (req, res) {
res.json({greeting: 'hello API'});
});
////////////////////////////////////////////////////////////////////////////////////
mongoose.connect(process.env.MONGO_URI);
var Schema = mongoose.Schema;
var url = new Schema({
name : {type:String},
shortUrl : {type:Number}
});
var Url = mongoose.model('Url', url);
app.post('/api/shorturl/new',
function(req,res){
var newUrl = new Url({name:req.body.url, shortUrl : 1});
newUrl.save(function (err, newUrl) {
if (err) return console.error(err);
console.log(newUrl.name+' saved sucessfully.');
});
res.json({name:req.body.url})
});
////////////////////////////////////////////////////////////////////////////////////
app.listen(port, function () {
console.log('Node.js listening ...');
});
But I got an error as below in the console and nothing saved in my database:
(node:127) DeprecationWarning: Mongoose: mpromise (mongoose’s default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html
I don’t understand the error and how can I debug my code?
Link to the challenge:
