Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: chanakaDe/Mint-REST
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0
Choose a base ref
...
head repository: chanakaDe/Mint-REST
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
  • 14 commits
  • 26 files changed
  • 5 contributors

Commits on Nov 4, 2015

  1. Copy the full SHA
    4da3eb2 View commit details

Commits on Nov 18, 2015

  1. Added node mailer to package json file

    chanaka_de_silva committed Nov 18, 2015
    Copy the full SHA
    28ea39b View commit details

Commits on Feb 26, 2016

  1. Merge pull request #1 from nevercast/patch-1

    Fix typo in api token expiration
    chanakaDe committed Feb 26, 2016
    Copy the full SHA
    de67ed7 View commit details

Commits on Feb 27, 2016

  1. added gitignore

    rody7val committed Feb 27, 2016
    Copy the full SHA
    4230746 View commit details

Commits on Feb 29, 2016

  1. Merge pull request #3 from rody7val/master

    added gitignore
    chanakaDe committed Feb 29, 2016
    Copy the full SHA
    c60b4cc View commit details

Commits on Mar 30, 2016

  1. Update README.md

    chanakaDe committed Mar 30, 2016
    Copy the full SHA
    55c4ec6 View commit details
  2. Update README.md

    chanakaDe committed Mar 30, 2016
    Copy the full SHA
    740999c View commit details
  3. Update README.md

    chanakaDe committed Mar 30, 2016
    Copy the full SHA
    d12d7db View commit details
  4. Update README.md

    chanakaDe committed Mar 30, 2016
    Copy the full SHA
    86be3e9 View commit details
  5. Update README.md

    chanakaDe committed Mar 30, 2016
    Copy the full SHA
    c0171de View commit details

Commits on Jul 30, 2016

  1. routes are spilited into seperate files and proper file structure is …

    …created
    Saravanan committed Jul 30, 2016
    Copy the full SHA
    634f1a3 View commit details

Commits on Jul 31, 2016

  1. forget password added

    Saravanan committed Jul 31, 2016
    Copy the full SHA
    7733c74 View commit details
  2. updated mail templates

    Saravanan committed Jul 31, 2016
    Copy the full SHA
    b045dd5 View commit details

Commits on Aug 1, 2016

  1. Merge pull request #9 from saravanan10393/master

    routes are spilited into seperate files and proper file structure is …
    chanakaDe authored Aug 1, 2016
    Copy the full SHA
    2e3e14c View commit details
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ndoe_modules
79 changes: 78 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Mint-REST

##### A simple rest api using Node.js , MongoDB , Express and Mongoose as the ORM.
##### A simple rest api using Node.js , MongoDB , Express and Mongoose as the ORM. Specially designed for blogging platforms and social netwroks.

### Overview

@@ -30,6 +30,83 @@ And make sure your MongoDB server is running.
See the config.js file for more configuration options.
You can change your app's secreet key , MongoDB host and also app's default port.

### Api documentation

#### Signup URL
http://localhost:3000/api/signup
This is the object have to passed.
```javascript
{
name: "",
username: "",
email: "",
password: ""
}
```

#### Login URL
http://localhost:3000/api/login
```javascript
{
username: "",
password: ""
}
```

#### Users get all URL
http://localhost:3000/api/users

#### Stories get all URL
http://localhost:3000/api/stories

#### Get specific story with story ID URL
http://localhost:3000/api/story?id=1

#### Delete specific story with story ID URL
http://localhost:3000/api/remove_story?id=1

#### Search specific story with story ID URL
http://localhost:3000/api/search_story?query=mongo

#### Search stories according to category URL
http://localhost:3000/api/search_story_by_category?category=java

#### Search specific user with email URL
http://localhost:3000/api/searchUserWithEmail?email=chanu1993@gmail.com

> To access following links, user must be logged into the system and has a token.
#### Create new story URL
http://localhost:3000/api/story
```javascript
{
owner: "",
title: "",
content: "",
category: "",
publishStatus: ""
}
```

#### Get all stories according to user ID URL
http://localhost:3000/api/story_of_user

#### Update story URL
http://localhost:3000/api/update_story
```javascript
{
owner: "",
title: "",
content: "",
category: "",
publishStatus: ""
}
```

### Procedure to run
* clone/download repository
* npm install
* node app.es6.js
### References
[http://projectslanka.blogspot.com/2015/10/create-restful-api-using-nodejs-part-2.html](http://projectslanka.blogspot.com/2015/10/create-restful-api-using-nodejs-part-2.html)

5 changes: 5 additions & 0 deletions app.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
let options = {
presets:['es2015']
}
require('babel-register')(options);
require('./app');
73 changes: 73 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict';
import express from 'express';
import path from 'path';
import favicon from 'serve-favicon';
import logger from 'morgan';
import cookieParser from 'cookie-parser';
import bodyParser from 'body-parser';
import cors from 'cors';
import config from './util/config';
import routes from './routes/index';

let app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

/**
* Using CORS to reduce CROSS Origine.
*/
app.use(cors());

app.use('/api', routes);

app.listen(config.port, (err) => {
if (err) {
console.log(err);
} else {
console.log("Listening on port 3000");
}
});

// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}

// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});


module.exports = app;
2 changes: 1 addition & 1 deletion app/routes/api.js
Original file line number Diff line number Diff line change
@@ -17,7 +17,7 @@ function createToken(user) {
username: user.username,
email: user.email
}, secretKey, {
expirtsInMinute: 1440
expiresInMinutes: 1440
});
return token;
}
90 changes: 90 additions & 0 deletions bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('sara:server');
var http = require('http');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/

var server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
19 changes: 19 additions & 0 deletions modules/db/schema/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import mongoose from 'mongoose';
import user from './user.schema';
import story from './story.schema';
import config from '../../../util/config';

mongoose.connect(config.dbUrl,(status) => {
console.log('mongodb connected',status);
});

let db = mongoose.connection;

db.on('error', (err) => console.log(err));

db.on('opne', (status) => console.log('db connection opened ',status));

let User = mongoose.model('User',user);
let Story = mongoose.model('Story',story);

export { User, Story};
15 changes: 15 additions & 0 deletions modules/db/schema/story.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import mongoose from 'mongoose';
/**
* Creating Story Schema.
* @type {*|Schema}
*/
var StorySchema = new mongoose.Schema({
owner: String,
title: String,
content: String,
category: String,
publishStatus: String,
created: {type: Date, default: Date.now}
});

export default StorySchema;
34 changes: 34 additions & 0 deletions modules/db/schema/user.schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import mongoose from 'mongoose';
import bcrypt from 'bcrypt-nodejs';

var Schema = mongoose.Schema;

/**
* Creating user schema.
* @type {*|Schema}
*/
var UserSchema = new Schema({
name: String,
username: {type: String, required: true, index: {unique: true}},
email: {type: String, required: true, index: {unique: true}},
password: {type: String, required: true, select: false}
});
/**
* Encrypting the user password.
*/
UserSchema.pre('save', function (next) {
var user = this;
if (!user.isModified('password')) return next();
bcrypt.hash(user.password, null, null, function (err, hash) {
if (err) return next(err);
user.password = hash;
next();
});
});

UserSchema.methods.comparePassword = function (password) {
var user = this;
return bcrypt.compareSync(password, user.password);
};

export default UserSchema;
Loading