Created
January 7, 2022 02:04
-
-
Save stephepush/2b233cfa136d2c118ea9e8d9c8e80ffd to your computer and use it in GitHub Desktop.
Various attempts to get an object to have an incrementing Id property
This file contains 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
[ | |
{"id":1,"url":"http://www.ibm.com","cutUrl":"www.ibm.com"}, | |
{"id":1,"url":"http://www.cnn.com","cutUrl":"www.cnn.com"}, | |
{"id":1,"url":"https://www.cnn.com","cutUrl":"www.cnn.com"}, | |
{"id":1,"url":"https://www.nintendo.com","cutUrl":"www.nintendo.com"} | |
] | |
//No matter what, the id is always 1 pretty much |
This file contains 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
app.post('/api/shorturl/', body('url').isURL(), | |
(req, res) => { | |
const errors = validationResult(req); | |
if(errors.isEmpty()){ | |
//console.log(req.body) | |
let url = req.body.url | |
const url_without_protocol = url.replace(/^https?:\/\//, '') | |
//above line source: https://stackoverflow.com/a/8206279/5456075 | |
console.log(url_without_protocol) | |
dns.lookup(url_without_protocol, (err, address, family)=>{ | |
console.log(`address: ${address} family: IPv${family}`) | |
}) | |
const shortenedLink = new ShortenedLink(req.body.url, url_without_protocol) | |
shortenedLink.save() | |
res.redirect(url) | |
} else { | |
res.send({error: 'invalid url'}) | |
} | |
}) |
This file contains 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
const fs = require('fs'); | |
const path = require('path'); | |
module.exports = Id =(() => { | |
let lastId= 0; | |
return class ShortenedLink { | |
constructor(url, cutUrl){ | |
this.id = ++lastId | |
this.url = url; | |
this.cutUrl = cutUrl | |
} | |
static incrementId() { | |
if(!this.latestId) this.latestId = 1 | |
else this.latestId++ | |
return this.latestId | |
} | |
save(){ | |
const p = path.join( | |
path.dirname(require.main.filename), | |
'data', | |
'shortenedLinks.json' | |
); | |
fs.readFile(p, (err, fileContent) =>{ | |
let links = [] | |
if (!err) { | |
links = JSON.parse(fileContent) | |
} | |
links.push(this) | |
fs.writeFile(p, JSON.stringify(links), (err)=>{console.log(err)}) | |
}) | |
} | |
static fetchAll(){ | |
const p = path.join( | |
path.dirname(require.main.filename), | |
'data', | |
'shortenedLinks.json' | |
); | |
fs.readFile(p, (err,fileContent)=>{ | |
if (err){ | |
return []; | |
} | |
return JSON.parse(fileContent) | |
}) | |
return links | |
} | |
}})(); | |
/* | |
last snippet at this link | |
*/ |
This file contains 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
const fs = require('fs'); | |
const path = require('path'); | |
module.exports = class ShortenedLink { | |
constructor(url, cutUrl){ | |
this.id = ShortenedLink.incrementId() | |
this.url = url; | |
this.cutUrl = cutUrl | |
} | |
static incrementId() { | |
if(!this.latestId) this.latestId = 1 | |
else this.latestId++ | |
return this.latestId | |
} | |
save(){ | |
const p = path.join( | |
path.dirname(require.main.filename), | |
'data', | |
'shortenedLinks.json' | |
); | |
fs.readFile(p, (err, fileContent) =>{ | |
let links = [] | |
if (!err) { | |
links = JSON.parse(fileContent) | |
} | |
links.push(this) | |
fs.writeFile(p, JSON.stringify(links), (err)=>{console.log(err)}) | |
}) | |
} | |
static fetchAll(){ | |
const p = path.join( | |
path.dirname(require.main.filename), | |
'data', | |
'shortenedLinks.json' | |
); | |
fs.readFile(p, (err,fileContent)=>{ | |
if (err){ | |
return []; | |
} | |
return JSON.parse(fileContent) | |
}) | |
return links | |
} | |
} | |
/* | |
Based on code from this s/o response: | |
https://stackoverflow.com/a/41916397/5456075 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment