1/* ====== create node.js server with express.js framework ====== */
2// dependencies
3const express = require("express");
4
5const app = express();
6
7app.get("/", (req, res) => {
8 res.send("This is home page.");
9});
10
11app.post("/", (req, res) => {
12 res.send("This is home page with post request.");
13});
14
15// PORT
16const PORT = 3000;
17
18app.listen(PORT, () => {
19 console.log(`Server is running on PORT: ${PORT}`);
20});
21
22
23// ======== Instructions ========
24// save this as index.js
25// you have to download and install node.js on your machine
26// open terminal or command prompt
27// type node index.js
28// find your server at http://localhost:3000
1const express = require('express')
2const app = express()
3const port = 3000
4
5app.get('/', (req, res) => {
6 res.send('Hello World!')
7})
8
9app.listen(port, () => {
10 console.log(`Example app listening at http://localhost:${port}`)
11})
1const express = require("express")
2
3const app = express()
4
5app.listen(5000, () => {
6 console.log("Server has started!")
7})
1There are several steps for setting up a basic Express Server:
2
31. Run npm init -y
42. Install your dependencies (express)
53. Create a .gitignore file
64. Add node_modules to your .gitignore
75. Create the server directory
86. Create your index file
97. Require your dependencies
108. Declare your app variable
119. Declare your listen port
1210. invoke the listen method and add a console log to the callback
1311. run nodemon server/index.js in your terminal
1412. success
15
16