Skip to content

Commit

Permalink
Merge pull request #459 from CodeYourFuture/content/node
Browse files Browse the repository at this point in the history
Content: Node port

Merging this and will update the workshops when they are approved
  • Loading branch information
SallyMcGrath authored Dec 28, 2023
2 parents cf855e9 + 08282b4 commit 0a03974
Show file tree
Hide file tree
Showing 57 changed files with 1,092 additions and 229 deletions.
7 changes: 7 additions & 0 deletions content/en/blocks/wordle/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
+++
title="Play Wordle"
headless="true"
time= 5
+++

<iframe src="https://www.nytimes.com/games/wordle/index.html" width="100%" height="640px"></iframe>
8 changes: 0 additions & 8 deletions content/en/node/_index.md

This file was deleted.

11 changes: 0 additions & 11 deletions content/en/node/blocks/block1/index.md

This file was deleted.

11 changes: 0 additions & 11 deletions content/en/node/blocks/block2/index.md

This file was deleted.

11 changes: 0 additions & 11 deletions content/en/node/blocks/block3/index.md

This file was deleted.

11 changes: 0 additions & 11 deletions content/en/node/prep/index.md

This file was deleted.

9 changes: 0 additions & 9 deletions content/en/node/product/_index.md

This file was deleted.

10 changes: 0 additions & 10 deletions content/en/node/product/build/index.md

This file was deleted.

10 changes: 0 additions & 10 deletions content/en/node/product/plan/index.md

This file was deleted.

10 changes: 0 additions & 10 deletions content/en/node/product/ship/index.md

This file was deleted.

10 changes: 0 additions & 10 deletions content/en/node/product/test/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/1/day-plan/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/1/prep/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/2/day-plan/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/2/prep/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/3/day-plan/index.md

This file was deleted.

14 changes: 0 additions & 14 deletions content/en/node/sprints/3/prep/index.md

This file was deleted.

16 changes: 0 additions & 16 deletions content/en/node/sprints/4/day-plan/index.md

This file was deleted.

8 changes: 8 additions & 0 deletions content/en/servers/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
+++
title = 'Servers'
description = 'Write server-side JavaScript with Node.js; develop CRUD applications with Express.js; write and test APIs with Postman'
layout = 'module'
emoji= '🔌'
menu = ['syllabus']
weight='7'
+++
90 changes: 90 additions & 0 deletions content/en/servers/blocks/building-the-server/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
+++
title = 'Building the server'
headless = true
time = 30
facilitation = false
emoji= '🧩'
[objectives]
1='Import the Express package in a file'
+++

The first thing we need to do is build our server. You will often need to build a server when writing back-end code. You can write a server in plain JavaScript, but Express is simpler to work with.

#### 1. Create a `server.js` file

Let's build our server! In your project, create a new file called `server.js`. This is where all our server code is going to live.

```zsh
touch server.js
```

#### 2. `import` the `express` library

We just installed Express, but we need to make sure it is included in this file specifically so we can use its methods. In Node.js, when you want to use Express in another file, you must `import` it.

To require Express, write the following inside `server.js`:

```js
import express from "express";
```

{{<note type="warning" title="CommonJS legacy">}}
Sometimes you will see `require` instead of `import`. This is because `require` is the old (CJS) way of importing packages in Node.js and not all environments (like runkit) are updated yet. If you see `require` in the curriculum, probably use `import` instead.

CJS syntax: `const express = require("express");`
MJS syntax: `import express from "express";`
{{</note>}}

#### 3. Initialise the server

To initialise our server, we need to call the `express()` function. This will create an Express application for us to work with.

Add the second line of code to your `server.js` file:

```js
const express = require("express");
const app = express();
```

#### 4. Start 'listening' for potential requests

One more step left, we need to set a **port** for our server to listen to. Think of a port as a door number: any requests that come to the server will come via that door. Setting a port will allow us to find where our server is running.

We use the **`app.listen`** method to do this. This method takes two arguments: a **port** and a **callback function** telling it what to do once the server is running.

> Need clarification? Read more about the `app.listen` method in the [Express documentation](http://expressjs.com/en/4x/api.html#app.listen).
We're going to run our server on port `3000`, and add a `console.log` in the callback function. Update your `server.js` file, calling the `app.listen` method:

```runkit
const express = require("express");
const app = express();
app.listen(3000, () => {
console.log("Server is listening on port 3000. Ready to accept requests!");
});
```

#### 5. Switch the server on!

You've built your server, but it isn't running yet. We need to run a command in
the terminal to do this. We are going to use the `node` keyword to run the
server file.

Type the following command in your terminal:

```sh
node server.js
```

If you see this, congratulations! You have built yourself a server!

![success](https://raw.githubusercontent.com/node-girls/workshop-cms/master/readme-images/step2-server02.png)

#### 6. npm script

To exit the running the server, type `ctrl + c`. Instead of running the server with `node server.js` everytime, we can create an alias for it in `package.json`.

Under the `scripts` property, add `start: node server.js`. We can now run our server using `npm start` which will be an alias (a shortcut) to `node server.js`.

Go to the terminal and type `npm start` and make sure that the server still runs.
Loading

0 comments on commit 0a03974

Please sign in to comment.