LoginSignup
0
0

More than 5 years have passed since last update.

Node.js express メモ Memo

Last updated at Posted at 2017-11-23

Node.js and Express (+nodemon)

$ mkdir express-server
$ cd express-server/
$ yarn init -y
$ yarn add express
$ yarn install

create a server.js file and copy and paste↓

from this website
http://expressjs.com/en/starter/hello-world.html

server.js
const express = require('express')
const app = express()

app.get('/', (req, res) => {
res.send('Hello World!');
});

app.listen(3000, () =>
console.log('Example app listening on port 3000!'));
$ yarn add nodemon --env
$ node server.js

command + T  ⇨ with running server,

$ curl localhost:3000

Hello World!

Go to http://localhost:3000/

It should show...
Hello World!


post & get
Edit like this

server.js
const express = require('express')
const app = express()

app.get('/', (req, res) => {
    res.send('This is a GET\n');
});

app.post('/', (req, res) => {
    res.send('This is a POST\n');
});

app.listen(3000, () =>
console.log('Example app listening on port 3000!'));

Then run

$ curl -X POST localhost:3000
This is a POST

And open it in browse http://localhost:3000/
It should show ⇨ This is a GET

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0