3 ms·
Ehhh yes and no. I still run into a lack of "it just works" and stays that way in JS land. It is likely I have not found the right combination of tools or the
by leetrout 3y ago
Ehhh yes and no.
I still run into a lack of "it just works" and stays that way in JS land. It is likely I have not found the right combination of tools or the right framework but nothing has been as reliable as django and the builtin contribs (auth, admin, sessions).
What would you recommend I look at in JS land knowing I love django and dislike flask?
- andrewstuart 3y ago>> What would you recommend I look at in JS land knowing I love django and dislike flask? I just use typescript and nodejs and the database. No framework except the express web server. It's incredibly raw and close to the metal, which I like. Here's a throwaway example of register and login from ChatGPT to illustrate how easy it is. Totally unchecked but it's just to show approximately what's involved - and it's not much at all. import express, { Request, Response } from 'express'; import bcrypt from 'bcrypt'; import db from './db'; const router = express.Router(); router.post('/register', async (req: Request, res: Response) => { const { username, password } = req.body; // Hash the password const saltRounds = 10; const hashedPassword = await bcrypt.hash(password, saltRounds); // Insert user into the database db.run('INSERT INTO users (username, password) VALUES (?, ?)', [username, hashedPassword], (err) => { if (err) { console.error(err.message); return res.status(500).json({ error: 'Internal Server Error' }); } return res.status(201).json({ message: 'User registered successfully' }); }); }); router.post('/login', async (req: Request, res: Response) => { const { username, password } = req.body; // Check if user exists db.get('SELECT * FROM users WHERE username = ?', [username], async (err, row) => { if (err) { console.error(err.message); return res.status(500).json({ error: 'Internal Server Error' }); } if (!row) { return res.status(401).json({ error: 'Invalid username or password' }); } const isPasswordValid = await bcrypt.compare(password, row.password); if (!isPasswordValid) { return res.status(401).json({ error: 'Invalid username or password' }); } return res.status(200).json({ message: 'Login successful' }); }); }); export { router as routes }; For database I use the postgres.js library which is super nice https://github.com/porsager/postgres https://github.com/porsager/postgres examples from postgres.js front page: import sql from './db.js' async function getUsersOver(age) { const users = await sql` select name, age from users where age > ${ age } ` // users = Result [{ name: "Walter", age: 80 }, { name: 'Murray', age: 68 }, ...] return users } async function insertUser({ name, age }) { const users = await sql` insert into users (name, age) values (${ name }, ${ age }) returning name, age ` // users = Result [{ name: "Murray", age: 68 }] return users }
- krembo 3y agoWell, for me this is less way readable than python. And I know JS for a long time, yet IMHO its syntax and structure seems clunky.
- berkes 3y agoReadability and unfamiliarity are two different things.
- PrimeMcFly 3y agoIt doesn't seem that close to the metal, and not enough to justify the lack of convenience and readability. It's like using C++ instead of C#. Still pretty removed.
- leetrout 3y agoRespectfully, and thank you for taking the time to reply, this is why I clarified I like Django and dislike Flask. I'm not interested in writing all my sql for every operation. I will gladly take the overhead of an ORM for the convenience in centralizing query building, table definition, object mapping and (in the case of django) migration tooling. Knowing it's battle tested saves me from my own silly mistakes. I do love sql alchemy so I can imagine enjoying something like Prisma but I suspect express is going to leave me wanting more. I think my next project will use Go to force myself to avoid Django and pick up a nice perf boost with much better type safety than Python + MyPy.