A base REST API project using Express and Node
Install Node.js using this gist.
Clone repo.
$ git clone git@github.com:MichaelCurrin/express-rest-quickstart.git
$ cd express-js-create-app-quickstart
npm install
Start local dev server.
$ npm start
Open in the browser:
While the server is running, you can run these in another terminal tab.
$ curl http://localhost:3000/
$ curl http://localhost:3000/foo
$ curl http://localhost:3000/foo -X POST
$ # Form data.
$ curl http://localhost:3000/foo -X POST -d 'a=b'
$ # JSON data.
$ curl http://localhost:3000/foo -X POST -d '{"a": "b"}' -H "Content-Type: application/json"
$ curl http://localhost:3000/foo -X DELETE
$ curl http://localhost:3000/foo/123
$ curl http://localhost:3000/bar
$ # Verbose flag shows the error code.
$ curl -v http://localhost:3000/baz
$ curl -v http://localhost:3000/admin
req.body
req.url
req.headers
req.query
req.params
You do not need use use bodyparser
package anymore to handle JSON body - see resources.
The .urlencoded
middleware is needed to parse the form data. You pass explicitly pass it extended
option (default is true
), to avoid a deprecation warning. See express #3650.
The .json
middleware parse JSON-formatted text for bodies with a Content-Type of application/json
.
- expressjs.com homepage.
- express package on NPM.
- Express docs
- Posts
- You may not need bodyparser
- Get HTTP POST Body in Express.js
- How to setup Express JS server - includes how to test in multiple ways
Released under MIT by @MichaelCurrin.