Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react2-week2/shahnawaz #328

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"presets": ["@babel/preset-env", "@babel/preset-react"],
"plugins": [
"@babel/plugin-transform-runtime",
"@babel/plugin-proposal-class-properties"
]
}
17 changes: 17 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Database - change to fit your database
DB_HOST = 0.0.0.0
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to always add this file to git ignore. It should not be committed to the repository since it contains sensitive information (password etc)

DB_USER = root
DB_PASSWORD = password1
DB_NAME = Meals
DB_PORT = 3306

NODE_ENV=development

# Your API will run on this port
API_PORT="5000"

# The path the API will run from
API_PATH="/api"

# Your Client will run on this port
CLIENT_PORT="3000"
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
node_modules
.DS_Store
Binary file added 6_nodejs/week3/.gitignore
Binary file not shown.
62 changes: 62 additions & 0 deletions 6_nodejs/week3/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@




const knex = require("knex")({
client: "mysql2",
connection: {
host: process.env.DB_HOST || "127.0.0.1",
port: process.env.DB_PORT || 3306,
user: process.env.DB_USER || "root",
password: process.env.DB_PASSWORD || "password1",
database: process.env.DB_NAME || "hyf_node_week3_warmup",
multipleStatements: true,
},
});

const express = require("express");
const app = express();
const port = process.env.PORT || 3000;

app.use(express.json());

const apiRouter = express.Router();
app.use("/api", apiRouter);

const contactsAPIRouter = express.Router();
apiRouter.use("/contacts", contactsAPIRouter);


// URL with injections for test http://localhost:3000/api/contacts?sort=last_name;%20DROP%20TABLE%20contacts;%20DESC

contactsAPIRouter.get("/", async (req, res) => {
let query = knex.select("*").from("contacts");

if ("sort" in req.query) {
const orderBy = req.query.sort.toString();
if (orderBy.length > 0) {
const [column, order] = orderBy.split(' ');
if (column && order && (order.toUpperCase() === 'ASC' || order.toUpperCase() === 'DESC')) {
query = query.orderBy(column, order);
} else {
res.status(400).json({ error: "Invalid sort parameter" });
return;
}
}
}

const sql = query.toSQL().toNative();
console.log("SQL", sql);

try {
const data = await query;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the correct way to call query should be query(), i.e. with brackets. But I'm not fully sure about this - it might be one of the new JavaScript syntax features.

In general, for backward compatibility, I recommend using empty brackets whenever you make a function call that takes no parameters :)

res.json({ data });
} catch (e) {
console.error(e);
res.status(500).json({ error: e.message });
}
});

app.listen(port, () => {
console.log(`Listening on port ${port}`);
});
Loading
Loading