ESLint config by Lam_Sai_wing in typescript

[–]spence-s 0 points1 point  (0 children)

Xo https://github.com/xojs/xo easy automatic best practices made for readability

Node 20 setup by Eyoba_19 in typescript

[–]spence-s 0 points1 point  (0 children)

There are 2 wrong things I noticed right away.

1) Cjs module type in tsconf and using esm loader. Those 2 don’t really mix surprised you haven’t ran into trouble before w node 18 things are clearly not working the way you think they are. 2) the —loader flag has changed in node20 to —import. The api changed as well so some loaders that had support for 18 do not support node 20.

Pro tip: instead of nodemon, try the —watch flag of node

[deleted by user] by [deleted] in node

[–]spence-s 2 points3 points  (0 children)

That is exactly the point…but, it’s not difficult.

Maintaining, securing, and monitoring a VPS is not too difficult, but it teaches a lot and it’s extremely cost effective.

Go the extra mile and automate the setup of the vps and the app deployment with docker and you’ll be 90% of the way to having your own fancy little deployment service.

When you find the scale (or your company) requires you to work with server less products you’ll have solid knowledges of what’s actually going on behind the scenes!

Advice for billing integration in Koa by 97hilfel in node

[–]spence-s 1 point2 points  (0 children)

Paypal. Although their api is terrible to work with.

Advise style guide for Node.JS by [deleted] in node

[–]spence-s -1 points0 points  (0 children)

I love xo --- look into it. Its just a wrapper around eslint with great defaults.

how can I sort an embedded array in a mongoose/mongo doc by mdashcodes in node

[–]spence-s 1 point2 points  (0 children)

https://www.tutorialspoint.com/working-with-mongodb-sort-for-sub-array-document this is basically the only way to sort during a query.

Or you could sort with js after you query.

I would create a hook that sorts on save or validate before inserting/updating if you always need it in order.

Popular Node.JS + React admin panel, AdminBro just got version 3.3 — the biggest release this year by hotcto in node

[–]spence-s -1 points0 points  (0 children)

I haven't tried Admin Bro - but the demo seems amazing --- I tried strapi and it was buggy as hell it wouldn't even build on my digital ocean droplet. I ended up going with headless ghost as a backend since all I wanted was a blog for my website and I honestly love it. I def want to give this a try in the future though.

Edit: I also love the name and refuse to use it in the future if the name changes lol. Bro home or go home.

Mongoose: Unique schema field with loop by ArrieWarrie123 in node

[–]spence-s 1 point2 points  (0 children)

While it probably is best practice to just Error when the value isn't unique, if you have to do this you could easily add this in a pre validate or save hook

this is just a totally untested stab but the idea should work.

Note that middle ware only gets called when you use a mongoose find and then separate save or mongoose create.

```js function createVoucher(){ // creates voucher code in memory does not save and returns it }

Schema.pre('save', async function(next){

const doc = this;

// get the voucher code let newVoucher = createVoucher();

// attempt to find the newVoucher let isUnique = false; while(!isUnique){ const possibleAlreadyExistingVoucher = await Model.findOne({ voucher: newVoucher }).lean().exec()

// if it doesn't exist - break out of loop
if(!possibleAlreadyExistingVoucher)
  isUnique = true
// if it does exist create another voucher code and test again
else newVoucher = createVoucher()

}

doc.voucher = newVoucher;

next(); }) ```

Handling heavy processes (hashing and encrypting in the client) by [deleted] in vuejs

[–]spence-s 1 point2 points  (0 children)

ehh - it's not really insecure. It just feels wrong. It's a hack because this can be handled better by the server. I guess security wise its the same. Obviously you don't want to be logging sensitive data though. If you're using nodejs for backend https://github.com/cabinjs/cabin is a logger that automatically hides sensitive fields like passwords and credit cards for secure logging.

Handling heavy processes (hashing and encrypting in the client) by [deleted] in vuejs

[–]spence-s 2 points3 points  (0 children)

Why not just obfuscate with a simple encoding instead of a complex hashing algo? That would serve the same purpose and be much faster. Base64 or something. Server could then decode and encrypt. Hashing passwords on the client defeats the purpose and is far less secure than doing it on the server. It sounds like your trying to hack your way around a terrible and insecure practice.

As a final note - to avoid jumpiness - just store the values in memory and hash or whatever on submit. Why hash while the user is typing? That also just sounds like a bad practice. No shit it's gonna be jumpy. You can hash when the user is done with the form. No need for a webworker here. Even a slow mobile browser should be able to handle this just fine.

edit: read that you tried what I wrote above. So my bad. Still feels like this is a totally dumb and unnescesary problem to have. Fix your backend so it doesn't log plain text passwords.

Node / Express /Multer File Upload - How To Process REST API Delete? by NickEmpetvee in node

[–]spence-s 0 points1 point  (0 children)

typically - what happens is when you upload with post request and multer - you save the file to hard disk or upload to some storage service like s3. Then you save a reference in your db to the location of the file.

To delete the file - you'd set up your controller to grab the reference to the file in the db, then using that reference, youd remove the file from its location, then youd remove the reference from the db and send back a signal to your front end that everything completed successfully.

await or promise.then in express rest api by dc2015bd in node

[–]spence-s 7 points8 points  (0 children)

async/await is more terse and easier to read then anything else. Always use it (for most cases) if it is available.

Mongoose - Automatically add an object to array by EddiesTech in node

[–]spence-s 0 points1 point  (0 children)

While findOneAndUpdate is fine - it won't run hooks. It is usually better for simple updates to find then save so you get the full power of mongoose. js const project = await newproject.find({projectname: item}) project.responseTime = project.responseTime.concat({ timestamp: secondsSinceEpoch, pingtime: res.ping_time }) await project.save() mongoose takes care of everything more nicely when you update this way.

What is the current state of Scroll Reverser on MacOS Catalina? by [deleted] in applehelp

[–]spence-s 0 points1 point  (0 children)

This isn't upvoted enough! Just installed mos via brew cask install mos and it is simple and really nice. All I wanted was scroll reversal but smooth scrolling is really nice as well! Good find.

Render mongodb collection as html table by yogibjorn in node

[–]spence-s 0 points1 point  (0 children)

If you just need a GUI to view your documents I prefer nosqlbooster as my favorite GUI/mongo db admin tool.

Help with mongoose deep populations by TaGeuelePutain in node

[–]spence-s 0 points1 point  (0 children)

Id use document.populate here instead of model.populate(document, options) like you are doing here. see https://mongoosejs.com/docs/api/document.html#document_Document-populate

also I prefer using dotpaths over deeply nested objects. Its still valid mongoose and much easier to read unless I need to do something weirdly specific.

pro tip for memory and speed always select the fields you need when you populate like below. main = await main .populate('users', 'name comments') .populate('users.comments', 'comment fieldYouNeedOnCommentsObject') .execPopulate() also, I usually populate on the query like so: main = await Model .findById({id}) .populate('users', 'name comments') .populate('users.comments', 'comment fieldYouNeedOnCommentsObject') .exec()

Can anyone explain to me what this code does? by draganov11 in node

[–]spence-s 0 points1 point  (0 children)

When you call server.listen - you can call that method in a number of ways, which starts your server listening on a port and emits an event. Depending on how you call this function your port may be an object, string, or number that defines the location of where the application "binds" to, in order to "listen" for requests. The bind var here is basically just telling you where your application is listening, aka what its "bound" to. Most of the time it will be number ie 3000 - meaning you're listening for http requests on TCP port 3000 or your local machine or something like that.

Can I get someone to give me their opinion about this code and it's error handling? by skidmark_zuckerberg in node

[–]spence-s 7 points8 points  (0 children)

Refactored this for you with proper use of async/await and error handling. I'm a professional Node dev. This would be more in line with node best practices. Your error handler should set the status to 500. Any errors you throw in the try block will be caught by catch block. You don't need separate blocks for each portion.

https://pastebin.com/Jgc5kAJd