-
Notifications
You must be signed in to change notification settings - Fork 4
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
base: main
Are you sure you want to change the base?
Changes from all commits
a251a75
c17c8b1
8809686
58f22ed
cc4b552
e752aad
f108509
60eda70
27d6094
cd8a043
4acedba
6060a46
13c99ae
51bfd62
c4851be
5e93bb0
fd6b33e
bc05dad
83546f7
1a6cd12
c49be0b
ea2b53d
f840363
fe551dc
0fdbc39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" | ||
] | ||
} |
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 | ||
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" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
node_modules | ||
node_modules | ||
.DS_Store |
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the correct way to call query should be 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}`); | ||
}); |
There was a problem hiding this comment.
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)