In this project you work in 2 files: app.js and sql.js
What I did not get is why we use regular single quotes( ’ ') in one file for our sql queries and backticks () in the other?
More specific,
in app.js you have code like this:
app.get(’/strips’, (req, res, next) => {
db.all(‘SELECT * FROM Strip’, (err, rows) => {
if (err) {
res.sendStatus(500);
} else {
res.send({strips: rows});
}
});
});
and in sql.js, code like this, with backticks:
db.serialize(() => {
db.run(DROP TABLE IF EXISTS Strip);
db.run(`CREATE TABLE IF NOT EXISTS Strip(
id INTEGER PRIMARY KEY,
head TEXT NOT NULL,
body TEXT NOT NULL,
background TEXT NOT NULL,
bubble_type TEXT NOT NULL,
bubble_text TEXT NOT NULL DEFAULT "",
caption TEXT NOT NULL DEFAULT ""
);`);
});
What’s the reason this similar type of code gets handled differently?
Thanks in advance.
Without delving into your code, or project, we only need to look at what supports are available. Does SQL support back-ticks? (Doubtful). So we lean on JavaScript (ES6+) for syntax support.
That leads to one question at any given point in your code: Which interpreter is parsing this statement?
Not sure what you’re asking but this project uses body-parser dev, hope that’s the answer.
It’s also mentionned nowhere in the course so I am just curious what’s the reason behind this, as my code without the backticks didn’t work so it must be pretty relevant.
In hindsight my first post wasn’t all that clear because the code looks a little messy, so here it is again but shortened:
app.js single quotes: db.all('SELECT * FROM Strip' etc etc
and in sql.js, with backticks: db.run(`` CREATE TABLE IF NOT EXISTS etc etc ``)
Sadly, this is not in my wheelhouse as I know nothing about Express, Mocha, Node or SQL beyond enough to do damage. At my age I’m sticking to learning things I can tinker with, not the serious stuff young people need to kick start their career.
Going to watch the walk through video to see if any wisdom can be gleaned from that.
console.log(`Server is listening on port: ${PORT}`)
It occurs to me that Node should support interpolation since it is JS (server-side, mind). That means sqlite3 also supports it, one would think. Pure speculation on my part.
db.run(`CREATE TABLE IF NOT EXISTS Strip`)
The above is a SQL statement (query?), not a string literal, hence the back-ticks.