Giter Club home page Giter Club logo

api-rest-with-nodejs's Introduction

Api Rest with NodeJs

Steps to create this project.

run in the console: npm init -y

Install

npm install express mongodb
npm install --save-dev @babel/core @babel/cli @babel/preset-env
npm install --save @babel/polyfill

Create .babelrc file with:

{
  "presets": [
    "@babel/preset-env"
  ]
}

Install

npm install --save-dev @babel/node

Create src folder and index.js inside the folder, with next code:

const test = 'test';
console.log(test);

Add into the package.json under the scripts key:

"build": "babel src --out-dir dist"

run in console npm run build. this should create an index.js file, with supported code, inside a dist directory.

Add into the package.json under the scripts key:

"start": "node dist/index.js"

run in console npm run start, this after building the project.

Install

npm install -D nodemon

Add into the package.json under the scripts key:

"dev": "nodemon src/index.js --exec babel-node"

delete dist folder and run:

npm run dev and you'll see this output:

...
> nodemon src/index.js --exec babel-node

[nodemon] 1.19.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `babel-node src/index.js`
test
[nodemon] clean exit - waiting for changes before restart

package.json

{
  "name": "rest-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "babel src --out-dir dist",
    "dev": "nodemon src/index.js --exec babel-node",
    "start": "node dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@babel/polyfill": "^7.4.4",
    "express": "^4.17.1",
    "mongodb": "^3.2.6"
  },
  "devDependencies": {
    "@babel/cli": "^7.4.4",
    "@babel/core": "^7.4.5",
    "@babel/node": "^7.4.5",
    "@babel/preset-env": "^7.4.5",
    "nodemon": "^1.19.1"
  }
}

Server

Create server.js file into src with:

import express from 'express';

const app = express();

app.get('/', (req, res) => res.send('Hello world'));

export default app;

and change index.js file content with:

import '@babel/polyfill';

import app from  './server';

const PORT = 3000;
 const main = async () => {
  await app.listen(PORT);
  console.log(`Server on port, ${PORT}`);
}

main();

Install

npm install dotenv --save-dev

Add to server.js:

if (process.env.NODE_ENV != 'production') {
  require('dotenv').config();
}

Create .env file with:

NODE_ENV=<environment_name>
PORT=<port_number>
DB_HOST=<db_host_name>
DB_PORT=<db_port_number>

Create database.js with:

import MongoClient from 'mongodb';

const DB_HOST = process.env.DB_HOST || 'localhost';
const DB_PORT = process.env.DB_PORT || '27017';

export const connect = async () => {
  try{
    const client = await MongoClient.connect(
      `mongodb://${DB_HOST}:${DB_PORT}`,
      { useNewUrlParser: true });
    const db = client.db('node-api-rest');
    console.info('DB is connected');

    return db;
  } catch(e) {
    return console.error(e);
  }
}

api-rest-with-nodejs's People

Contributors

joalbertg avatar

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.