Skip to content
Merged
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
4 changes: 2 additions & 2 deletions courses/foundation/intro-to-backend/exercises/exercise1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Exercise 1

Navigate to `intro-to-backend` in your assignment repo and open it in your code editor, where you'll find some exercises ready to complete.
Navigate to `intro-to-backend` in your [assignment repo](https://github.com/HackYourFuture-CPH/hyf-assignment-template/tree/main/courses/foundation/intro-to-backend) and open it in your code editor, where you'll find some exercises ready to complete.

## Getting started

Expand All @@ -9,7 +9,7 @@ Let's start by taking a look at `exercise1.js`. Here you'll find a simple webser
Run the webserver to see it in action:

```shell
nodemon exercise1.js
node --watch exercise1.js
```

Go to <http://localhost:3000> in your browser to verify that everything is working as expected. You should see a page that displays the same string being returned in the "/" route function.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Session materials

## Contents

1. `index.js` file is a playground for live coding during the session. It is deliberately empty.
2. `/examples` folder contains code inspirations to demonstrate during session to showcase possibilies.
- `pages.js` show how simple HTML can be passed via endpoints
- `math.js` show that endpoints can get passed variables and perform operations
- `data.js` show how to get access to the data from a database
3. Postman collection JSON

---

## Node JS

### Getting started

1. Navigate to directory in your console
2. Run `npm i`
3. Run `npm run start`

#### Tip

If you prefer, you might run examples directly, using:

```bash
node --watch examples/[YOUR CHOSEN EXAMPLE].js
```

## Postman

1. Open Postman app
2. Click on `Import` button next to your user name
3. Import the collection from the repo
4. Make sure your Node server is running to access needed endpoints
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import express from "express";
import knexLibrary from "knex";

const dbFile = "../../../databases/week1/assets/test.sqlite3";
// NOTE: if you didn't clone the entire programme repo
// replace the above path with actual path to your sqlite instance

const knex = knexLibrary({
client: "sqlite3",
connection: {
filename: dbFile,
},
});

const app = express();

app.use(express.json());

app.get("/", async (request, response) => {
const { query } = request;

if (query.table) {
// for example: http://localhost:3000/?table=task
const data = await knex.raw(`select * from ${query.table}`);
return response.json(data);
}

const tables = await knex.raw(`SELECT name FROM sqlite_schema`);
response.json(tables);
});

app.post("/status", async (request, response) => {
const { id, name } = request.body;

if (!id || !name || name.length === 0) return response.sendStatus(400);

await knex.raw(`insert into status(id, name) values(${id}, "${name}")`);
response.sendStatus(200);
});

app.listen(3000, function () {
console.log(`> Ready on http://localhost:3000`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import express from "express";
const app = express();

app.get("/", (_request, response) => {
response.send("Let's do some math!");
});

app.get("/add", (request, response) => {
console.log("Your query: " + request.query);

if (!request.query.a && !request.query.b)
return response.send(`
<h1> No values to add! </h1>
Append <code>?a=2&b=3</code> to the url
`);

const { a, b } = request.query;
const sum = Number(a) + Number(b);

response.send(sum);
});

app.listen(3000, function () {
console.log(`> Ready on http://localhost:3000`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import express from "express";
const app = express();

app.get("/", (_request, response) => {
response.send(`
<body>
<h1>Main page</h1>
<h2>Shows main content</h2>
</body>
`);
});

app.get("/about", (_request, response) => {
response.send(`
<body>
<h1>About</h1>
<p>This is an about page</p>
</body>
`);
});

app.get("/contact", (_request, response) => {
response.send(`
<body>
<h1>Contact</h1>
<p>This is a contact page</p>
<form>
<label>name</label>
<input placeholder="Write your name">
<button>Contact me</button>
</form>
</body>
`);
});

app.listen(3000, function () {
console.log(`> Ready on http://localhost:3000`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from "express";
const app = express();

app.get("/", (_request, response) => {
response.send("hello");
});

app.listen(3000, function () {
console.log(`> Ready on http://localhost:3000`);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

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

🤩

"info": {
"_postman_id": "f1c288ff-7ed3-482a-9c8e-a6dd5a99421d",
"name": "Intro to backend",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
"_exporter_id": "48182721",
"_collection_link": "https://magdazelena-ea3683e1-7432080.postman.co/workspace/Magdalena-Odrow%C4%85%C5%BC-%C5%BBelezik's-Wor~d3429c08-9437-4576-84c6-94f688cd37c5/collection/48182721-f1c288ff-7ed3-482a-9c8e-a6dd5a99421d?action=share&source=collection_link&creator=48182721"
},
"item": [
{
"name": "sample requests",
"item": [
{
"name": "sample get",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url}}/",
"host": ["{{url}}"],
"path": [""]
}
},
"response": []
}
]
},
{
"name": "database endpoints",
"item": [
{
"name": "get all tables",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url}}/",
"host": ["{{url}}"],
"path": [""]
}
},
"response": []
},
{
"name": "get data by table",
"request": {
"method": "GET",
"header": [],
"url": {
"raw": "{{url}}/?table=status",
"host": ["{{url}}"],
"path": [""],
"query": [
{
"key": "table",
"value": "status"
}
]
}
},
"response": []
},
{
"name": "add status",
"request": {
"method": "POST",
"header": [],
"body": {
"mode": "raw",
"raw": "{\r\n \"name\": \"In progress\",\r\n \"id\": 4\r\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "{{url}}/status",
"host": ["{{url}}"],
"path": ["status"]
}
},
"response": []
}
],
"description": "Make sure your server is running.\n\nMake sure you have correct endpoints in index.js\n\nOtherwise make sure you ran: \n`node --watch examples/data.js`"
}
],
"event": [
{
"listen": "prerequest",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [""]
}
},
{
"listen": "test",
"script": {
"type": "text/javascript",
"packages": {},
"exec": [""]
}
}
],
"variable": [
{
"key": "url",
"value": "http://localhost:3000",
"type": "string"
}
]
}
Loading