Skip to content

Instantly share code, notes, and snippets.

Avatar
🌏
Amor vincit omnia.

Chanaka De Silva chanakaDe

🌏
Amor vincit omnia.
View GitHub Profile
View server.js
var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var config = require("./config");
var mongoose = require("mongoose");
var cors = require('cors');
var app = express();
/**
View user.js
var mongoose = require('mongoose');
var bcrypt = require('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}},
View story.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
/**
* Creating Story Schema.
* @type {*|Schema}
*/
var StorySchema = new Schema({
owner: String,
title: String,
View post.js
/**
* Create new user and save in database.
*/
api.post('/signup', function (req, res) {
var user = new User({
name: req.body.name,
username: req.body.username,
password: req.body.password
});
var toekn = createToken(user);
View get.js
/**
* Get all users from the database.
*/
api.get('/users', function (req, res) {
User.find({}, function (err, users) {
if (err) {
res.send(err);
return;
}
res.json(users);
View checkLogin.js
/**
* Check logged status in order to
* give permission to following links.
*/
api.use(function (req, res, next) {
console.log("Somebody logged into system");
var token = req.body.token || req.param('token') || req.headers['x-access-token'];
// Check if token exists.
if (token) {
View api.js
var User = require('../models/user');
var Story = require('../models/story');
var config = require('../../config');
var secretKey = config.secretKey;
var jsonwebtoken = require('jsonwebtoken');
/**
* Create new user token for verification.
* @param user
* @returns {number}
*/
View config.js
module.exports = {
"database":"mongodb://root:abc123@dbh70.mongolab.com:27707/userstory",
"port":process.env.PORT || 3000,
"secretKey" : "123456789"
};
View package.json
{
"name": "userstory",
"version": "1.0.0",
"description": "Sample app using MEAN stack",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Chanaka De Silva",
"license": "ISC",
View web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.chanakaSample</param-value>