Yaah waith A minitue

Usage examples:

JavaScript: Fetch API

fetch("https://type.fit/api/quotes")
  .then(function(response) {
    return response.json();
  })
  .then(function(data) {
    console.log(data);
  });

equire(‘dotenv’).config();
const express = require(‘express’);
const cors = require(‘cors’);
const app = express();

// Basic Configuration ********
const port = process.env.PORT || 3000;

//=== I tried to include my MONGO_URI code
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true} );
console.log(mongoose.connect.readyState)
// const mySecret = process.env[‘MONGO_URI’]
//=== ^ Is this how I should put the mongo code in here?

app.use(cors());
app.use(’/public’, express.static(${process.cwd()}/public));
app.get(’/’, function(req, res) {
res.sendFile(process.cwd() + ‘/views/index.html’);
});

//=== Your first API endpoint. This is where I know I should code here.
app.post(’/api/shorturl’, function(req, res) {
console.log(req.body);
const bodyurl = req.body.url;

const something = dns.lookup(urlparser.parse(bodyurl).hostname, (error, address) => {
if(!address) {
res.json({error: “Invalid URL”})
} else {
const url = new Url({ url: bodyurl })
url.save((error, data) => {
res.json({
original_url: data.url,
short_url: data.id
})
})
}
console.log(“dns”, error);
console.log(“address”, address);
})

console.log(“something”, something);
});

app.get("/api/shorturl/:id", (req, res) => {
const id = req.params.id;
Url.findById(id, (error, data) => {
if(!data){
res.json({error: “Invalid URL”})
} else {
res.redirect(data.url)
}
})
})

app.listen(port, function() {
console.log(Listening on port ${port});
});

Do you have a question? I don’t understand what replies you would like to this post.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.