Smaily
August 9, 2021, 3:23pm
1
Tell us what’s happening:
Hello there ,
I’m doing this challenge of shorting URLs in the FCC challenges so I had to use body parser in order to get the url from the input field , I followed the documentation but I didn’t get any result from the req.body
Your code so far
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
// Basic Configuration
const port = process.env.PORT || 3000;
app.use(cors());
app.use(bodyParser.json());
app.use('/public', express.static(`${process.cwd()}/public`));
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' });
});
// const Schema = mongoose.Schema;
// const ObjectId = Schema.ObjectId;
// const NewURL = new Schema({
// oldURL: String,
// sufix: String,
// newURL: String,
// date: Date
// });
app.post('/api/shorturl', (req,res)=>{
let url = req.body
console.log(req.body);
console.log(url)
res.json({"original_url": url})
} )
app.listen(port, function() {
console.log(`Listening on port ${port}`);
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36
Challenge: URL Shortener Microservice
Link to the challenge:
The data is comming from a form POST (x-www-form-urlencoded). Use the urlencoded
method instead, check the docs . You also want to get the url
property on body
and not save body to a url variable (so either destructuring the property or dotting on to it).
2 Likes
Smaily
August 9, 2021, 11:34pm
3
I tried that solution in VS Code before using Replit but it didn’t work too , it shows a line on bodyParser word
the Url to the file :
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const app = express();
var mongo = require('mongodb');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const validUrl = require('valid-url');
// Basic Configuration
const port = 3000;
// mongoose.connect( process.env.DB_URI , {useNewUrlParser: true, useUnifiedTopology: true}, () =>
// console.log("connected"));
mongoose.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
This file has been truncated. show original
And what do you mean by? I couldn’t get you
You also want to get the url property on body and not save body to a url variable (so either destructuring the property or dotting on to it).
If you hover over it you can see it’s just a deprecation warning. You can use the built-in middleware instead (in Express v4.16.0 onwards), but both will work.
Your code works for me. When I post using the form I see the object in the console and the res.json on the page.
I mean you want the url
property on body
.
const { url } = req.body
or
const url = req.body.url
system
Closed
February 8, 2022, 7:06pm
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.