Code Monkey home page Code Monkey logo

express-es6-rest-api's Introduction

Express & ES6 REST API Boilerplate

This is a straightforward boilerplate for building REST APIs with ES6 and Express.

Tip: If you are using Mongoose, you can automatically expose your Models as REST resources using restful-mongoose.

Getting Started

# clone it
git clone [email protected]:developit/express-es6-rest-api.git
cd express-es6-rest-api

# Make it your own
rm -rf .git && git init && npm init

# Install dependencies
npm install

# Start development live-reload server
PORT=8080 npm run dev

# Start production server:
PORT=8080 npm start

Docker Support

cd express-es6-rest-api

# Build your docker
docker build -t es6/api-service .
#            ^      ^           ^
#          tag  tag name      Dockerfile location

# run your docker
docker run -p 8080:8080 es6/api-service
#                 ^            ^
#          bind the port    container tag
#          to your host
#          machine port   

License

MIT

express-es6-rest-api's People

Contributors

adylevy avatar benrolfe avatar developit avatar gilesb2 avatar itsjw avatar osoianmarcel avatar previousdeveloper avatar tuomokar avatar zouhir avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

express-es6-rest-api's Issues

Suggestions for dependent resources?

I forked this and began a small app using postgres and knex, and I am trying to understand the best way to implement dependent resources, any thoughts? (For example I am trying to set up user's todos, and I can't seem to get the resource-router-middleware to do what I want).

My fork is here: flow_todos_api.

Any thoughts appreciated, thanks!

Any suggestion for debugging?

Hi, firstly thanks for boilerplate.

I have tried babel-node-debug with my own boilerplate but it didn't seem to work well to me. Have any suggestions?

Thanks.

Explanation on use of resource-router-middleware

Would you mind giving a quick explanation of what resource-router-middleware accomplishes in this boilerplate? (Like why include it instead of regular routing) Perhaps I'm a bit confused also on the use of the word 'facet' in this instance. Much appreciated, I would like to understand.

Deploying to production

Do you deploy this as a whole repo? Or just the Dist? I was deploying just the Dist, but would need to manage the proper node modules to distribute.

404 but the object is still returned

So, i am facing some really interesting problem:

i am just setting up a demo environment, using this boilerpalte:

in the models/facets.js i have created an object as the following

const facets = [
    { id: 1, title: "hello", category: "greeting" },
    { id: 2, title: "moro", category: "greeting" },
];
export default facets;

ten in the api/facets.js (after a little modification)

load(req, id, callback) {
		let thisId = parseInt(id);
		console.log(typeof thisId);
		let facet = facets.find( facet => facet.id === thisId ),
			err = facet ? facet : 'Not found';		
		callback(err, facet);
	},

Here I had to change the ternary operator, so instead of returning null i am returning the facet object which was found.

In this case, i am getting back the object { id: 1, title: "hello", category: "greeting" }.
If it stays as null the http response is 200, but the object is not coming back (since its null obviously..

What am i missing here?

Thank you in advance!

Question about middleware

Hi, Can you give me an example of how to use a middleware, for example I have a route '/users' and I have a middleware to restrict access to that route, should I configure it in 'middleware/index.js' ? how can I set that middleware only for that route?

Thanks

PUT \ POST routes Basic Auth

Would it be a good addition to have optional basic http auth on some routes?

I did that in my private fork and can clean up and send PR here

async/await syntax

I am using async/await syntax in my project. runs fine with PORT=8080 npm run dev but when i try to run via docker i get error
ReferenceError: regeneratorRuntime is not defined
do I need to modify babel?

Why database reference being posted as an argument in the example?

Hey,
Is this a common practice to initialize db on server start and then push it as am argument everywhere? I used to initialize it and expose it as a module to all other modules, so i can do something like this anywhere:

import {update} from '../database'

export default (req, res) => {
  // yet another api method
}

Is the solution from this repo somehow better? It doesn't look more convenient.

Thank you!

How to import models?

Hello,

First off, thank you for making this boilerplate! I just have a quick question regarding models. I'm making a React app with this boilerplate serving as the back end. Inside the models directory, I have my User.js file. It looks like I need to import that file into facets.js so that the schema can be registered, but I'm not sure how to do this?

Sorry if my question is elementary.

You can see my source here:
https://github.com/jonleopard/heartbreaker/tree/master/src/models

Cheers!

Where can I find a Mongoose ES6 example?

Sorry if this is off-topic, but I love the structure of express-es6-rest-api and I'm struggling to use it with Mongoose. If anyone can point me to an example, that'd be great.

I saw the restful-mongoose link in the README, but that's a bit too automagic to me, I need more control.

Deletion doesn't work

The response of server to delete request to "url/id" is "500 (internal server error)"

Docker Deployment: 404

Minor issue, but I tried viewing the docker demo at came across a 404 error. If there's anyway I can help, I wouldn't mind throwing a hand to getting it up and running again :)

babel-node

I'm curious if you know something different, or if this is a just-for-fun repo, ;-)

Not meant for production use
You should not be using babel-node in production. It is unnecessarily heavy, with high memory usage due to the cache being stored in memory. You will also always experience a startup performance penalty as the entire app needs to be compiled on the fly.

https://babeljs.io/docs/usage/cli/

Deploying to production

Do you deploy this as a whole repo? Or just the Dist? I was deploying just the Dist, but would need to manage the proper node modules to distribute.

No incoming http request at terminal

Hi,
With my old project, I see the incoming http requests at terminal like this:

POST /api/login 200 816 - 42.802 ms
POST /api/users/me 200 800 - 39.342 ms

But when I run this boilerplate, I see nothing at terminal, so can someone show me the way to enable terminal logs again? Very thanks!

My run script:

"scripts": {
    "dev": "nodemon -w server --verbose --exec 'NODE_ENV=development DEBUG=App,$DEBUG babel-node index.js --presets es2015,stage-3'",
    "start": "DEBUG=App,$DEBUG babel-node index.js",
    "test": "NODE_ENV=test mocha --trace-deprecation server/test/$file --compilers js:babel-core/register --timeout 20000 --slow 500"
  }

How to set environment variables?

Hi, I'm having troubles when I want to set environment variables.
I think that the best way is using diferent tasks on the script field inpackage.json to add this values but I don't know.

Do you know how I can create this task to get different values depends of the environment?

Thanks.

how to change port?

Start development live-reload server

PORT=8080 npm run dev

Start production server:

PORT=8080 npm start

This does not work.

Suggestions for testing?

Having all sorts of issues using this setup with Jest and Supertest. When I run multiple tests I get address already in use. Any suggestions on how you test the api calls?

"babel: not found" while deploying to heroku

I get the error "Babel not found" while deploying to heroku.

I see heroku is calling the prestart script when deploying, should this prestart script work in production? do I have to do something else?

Modified boilerplate with JWT authentication

I created custom boilerplate based upon this with simple login system connected to MongoDB and JWT authentication express-es6-rest-api-auth
Maybe someone of you will be interested in this. I know it's not good place to post this but I couldn't find a way to contact author of this boilerplate. You are welcome to link to mine repository as some form of "inspired" boilerplates.

Cannot find module babel-node

While running the installation instructions, I first ran into an issue where ../lib/cli wasn't found. Globally installing "nodemon" solved that issue, but now I'm stuck on this one. I'd ask this in a chat but I don't see any other means of contacting you all for assistance. :)

PORT=8080 npm run dev

> [email protected] dev /Volumes/Xenophoto/Dropbox/src/[redacted]
> nodemon -w src --exec "babel-node src --presets es2015,stage-0"

[nodemon] 1.10.0
[nodemon] to restart at any time, enter 'rs'
[nodemon] watching: /Volumes/Xenophoto/Dropbox/src/[redacted]/src/**/*
[nodemon] starting 'babel-node src --presets es2015,stage-0'
module.js:440
throw err;
^
Error: Cannot find module '../lib/babel-node'
at Function.Module._resolveFilename (module.js:438:15)
at Function.Module._load (module.js:386:25)
at Module.require (module.js:466:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/Volumes/Xenophoto/Dropbox/src/[redacted]/node_modules/.bin/babel-node:3:1)
at Module._compile (module.js:541:32)
at Object.Module._extensions..js (module.js:550:10)
at Module.load (module.js:456:32)
at tryModuleLoad (module.js:415:12)
at Function.Module._load (module.js:407:3)
[nodemon] app crashed - waiting for file changes before starting...

Async error

In index.js, the call to app.server.listen() is asynchronous, but a console output on the next line is expecting the process to have completed. It throws an error on my fresh install. I had to wrap the console output in the callback argument for server.listen()

app.server.listen(process.env.PORT || config.port, () => {
	console.log(`Started on port ${app.server.address().port}`);
});

instead of

app.server.listen(process.env.PORT || config.port);
console.log(`Started on port ${app.server.address().port}`);

How add custom API?

I want add custom api without 'get', 'read', 'put', 'patch', 'update', 'del', 'delete'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.