master 200267506f13 cached
8 files
3.7 KB
1.1k tokens
1 requests
Download .txt
Repository: adnanrahic/nodejs-restful-api
Branch: master
Commit: 200267506f13
Files: 8
Total size: 3.7 KB

Directory structure:
gitextract_f8of_lyu/

├── .gitignore
├── README.md
├── app.js
├── db.js
├── package.json
├── server.js
└── user/
    ├── User.js
    └── UserController.js

================================================
FILE CONTENTS
================================================

================================================
FILE: .gitignore
================================================
node_modules
.vscode
npm-debug.log
.serverless
variables.env

================================================
FILE: README.md
================================================
# nodejs-restful-api
![RESTful API design with Node.js](https://cdn-images-1.medium.com/max/2000/1*jjYC9tuf4C3HkHCP5PcKTA.jpeg "RESTful API design with Node.js")

How to create a RESTful CRUD API using Nodejs?

This tutorial will demo how to set up a bare bones 
API using mongodb as the database.

It consist of a User model and controller. The model
defines the data, and the controller will contain all 
the business logic needed to interact with the database. 

It has a db file which will be used to
connect the app to the database, and an app file used
for bootstrapping the application itself.

The server file is used to spin up the server and tells the
app to listen on a specific port.

Full tutorial can be found at:
https://hackernoon.com/restful-api-design-with-node-js-26ccf66eab09#.s5l66zyeu


================================================
FILE: app.js
================================================
var express = require('express');
var app = express();
var db = require('./db');

var UserController = require('./user/UserController');
app.use('/users', UserController);

module.exports = app;

================================================
FILE: db.js
================================================
var mongoose = require('mongoose');
mongoose.connect('mongodb://yourMongoDBURIGoesHere');

================================================
FILE: package.json
================================================
{
  "name": "nodejs-creating-restful-apis",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "start": "node server.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.18.2",
    "express": "^4.16.2",
    "mongoose": "^5.0.1"
  }
}


================================================
FILE: server.js
================================================
var app = require('./app');
var port = process.env.PORT || 3000;

var server = app.listen(port, function() {
  console.log('Express server listening on port ' + port);
});

================================================
FILE: user/User.js
================================================
var mongoose = require('mongoose');  
var UserSchema = new mongoose.Schema({  
  name: String,
  email: String,
  password: String
});
mongoose.model('User', UserSchema);

module.exports = mongoose.model('User');

================================================
FILE: user/UserController.js
================================================
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

router.use(bodyParser.urlencoded({ extended: true }));
router.use(bodyParser.json());
var User = require('./User');

// CREATES A NEW USER
router.post('/', function (req, res) {
    User.create({
            name : req.body.name,
            email : req.body.email,
            password : req.body.password
        }, 
        function (err, user) {
            if (err) return res.status(500).send("There was a problem adding the information to the database.");
            res.status(200).send(user);
        });
});

// RETURNS ALL THE USERS IN THE DATABASE
router.get('/', function (req, res) {
    User.find({}, function (err, users) {
        if (err) return res.status(500).send("There was a problem finding the users.");
        res.status(200).send(users);
    });
});

// GETS A SINGLE USER FROM THE DATABASE
router.get('/:id', function (req, res) {
    User.findById(req.params.id, function (err, user) {
        if (err) return res.status(500).send("There was a problem finding the user.");
        if (!user) return res.status(404).send("No user found.");
        res.status(200).send(user);
    });
});

// DELETES A USER FROM THE DATABASE
router.delete('/:id', function (req, res) {
    User.findByIdAndRemove(req.params.id, function (err, user) {
        if (err) return res.status(500).send("There was a problem deleting the user.");
        res.status(200).send("User: "+ user.name +" was deleted.");
    });
});

// UPDATES A SINGLE USER IN THE DATABASE
router.put('/:id', function (req, res) {
    User.findByIdAndUpdate(req.params.id, req.body, {new: true}, function (err, user) {
        if (err) return res.status(500).send("There was a problem updating the user.");
        res.status(200).send(user);
    });
});


module.exports = router;
Download .txt
gitextract_f8of_lyu/

├── .gitignore
├── README.md
├── app.js
├── db.js
├── package.json
├── server.js
└── user/
    ├── User.js
    └── UserController.js
Condensed preview — 8 files, each showing path, character count, and a content snippet. Download the .json file or copy for the full structured content (4K chars).
[
  {
    "path": ".gitignore",
    "chars": 60,
    "preview": "node_modules\n.vscode\nnpm-debug.log\n.serverless\nvariables.env"
  },
  {
    "path": "README.md",
    "chars": 807,
    "preview": "# nodejs-restful-api\n![RESTful API design with Node.js](https://cdn-images-1.medium.com/max/2000/1*jjYC9tuf4C3HkHCP5PcKT"
  },
  {
    "path": "app.js",
    "chars": 194,
    "preview": "var express = require('express');\nvar app = express();\nvar db = require('./db');\n\nvar UserController = require('./user/U"
  },
  {
    "path": "db.js",
    "chars": 89,
    "preview": "var mongoose = require('mongoose');\nmongoose.connect('mongodb://yourMongoDBURIGoesHere');"
  },
  {
    "path": "package.json",
    "chars": 359,
    "preview": "{\n  \"name\": \"nodejs-creating-restful-apis\",\n  \"version\": \"1.0.0\",\n  \"description\": \"\",\n  \"main\": \"app.js\",\n  \"scripts\": "
  },
  {
    "path": "server.js",
    "chars": 171,
    "preview": "var app = require('./app');\nvar port = process.env.PORT || 3000;\n\nvar server = app.listen(port, function() {\n  console.l"
  },
  {
    "path": "user/User.js",
    "chars": 212,
    "preview": "var mongoose = require('mongoose');  \nvar UserSchema = new mongoose.Schema({  \n  name: String,\n  email: String,\n  passwo"
  },
  {
    "path": "user/UserController.js",
    "chars": 1871,
    "preview": "var express = require('express');\nvar router = express.Router();\nvar bodyParser = require('body-parser');\n\nrouter.use(bo"
  }
]

About this extraction

This page contains the full source code of the adnanrahic/nodejs-restful-api GitHub repository, extracted and formatted as plain text for AI agents and large language models (LLMs). The extraction includes 8 files (3.7 KB), approximately 1.1k tokens. Use this with OpenClaw, Claude, ChatGPT, Cursor, Windsurf, or any other AI tool that accepts text input. You can copy the full output to your clipboard or download it as a .txt file.

Extracted by GitExtract — free GitHub repo to text converter for AI. Built by Nikandr Surkov.

Copied to clipboard!