Front End Developer Resume
Front End Developer Resume
Front End Developer Resume
What is Express.js?
ExpressJS Features:
operations.
templates.
request.
ExpressJS Framework.
{
"name": "npm_smart_grocery",
"version": "1.0.0",
"description": "a sample smart grocery manager ",
"main": "index.js",
"": {
"ajv": "^4.9.0",
"async": "^1.4.2",
"body-parser": "^1.13.3",
"cloudant": "^1.4.0",
"dotenv": "^2.0.0",
"express": "^4.13.3",
"express-session": "^1.11.3",
"memory-cache": "^0.1.4",
"moment": "2.10.6",
"passport": "^0.3.2",
"path-exists": "^3.0.0",
"r-script": "0.0.3",
"rio": "^2.4.1",
"rox": "0.0.1-13",
"superagent": "^1.3.0",
"twitter": "^1.4.0",
"underscore": "^1.8.3",
"v8": "^0.1.0",
"winston": "^2.1.1",
"winston-daily-rotate-file": "^1.0.1"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/dahlia05/npm_smart_grocery.git"
},
"author": "dahlia",
"license": "ISC",
"bugs": {
"url": "https://github.com/dahlia05/npm_smart_grocery/issues"
},
"homepage": "https://github.com/dahlia05/npm_smart_grocery#readme"
}
Open command prompt on the project folder and run following command.
Npm install
"use strict";
app.use(express.static(__dirname + '/app'));
/* Start server */
app.listen(app.get('port'), function() {
logger.info('Express server listening on http://' + app.get('host') + ':' +
app.get('port'));
});
module.exports = router;
Create a folder called ‘app’ inside the project folder and create a file inside ‘app’
folder called ‘index.html’.
Open command prompt on the project folder and run following command.
node server.js
/**
* Contains all application routes.
* @version 1.0
*/
'use strict';
module.exports = {
'/member/register': {
post: {
controller: 'MemberController',
method: 'register',
public: true
}
},
'/member/login': {
post: {
controller: 'MemberController',
method: 'login',
public: true
}
},
'/member/logout': {
post: {
controller: 'MemberController',
method: 'logout',
public: true
}
},
'/member/profile/:profileId': {
post: {
controller: 'MemberController',
method: 'saveProfile',
public: false
},
get: {
controller: 'MemberController',
method: 'getProfile',
public: false
},
delete: {
controller: 'MemberController',
method: 'deleteProfile',
public: false
}
}
};
/**
* Login.
* @param {Object} req the request
* @param {Object} res the response
* @param {Function} next the next middleware
*/
function login(req, res, next) {
var username = req.body.authInfo.username;
var password = req.body.authInfo.password;
async.waterfall([
function (cb) {
MemberService.loginMember(username,password, cb);
},
function (response, cb) {
var query = { selector: { email: username }, limit: 1};
DBService.find(query, cb);
},
function (data, cb) {
var user = {
"username": username,
"isLoggedIn": true
};
req.session.user = user;
res.json({
MemberInfo: user
});
cb();
}], function (err) {
if (err) {
req.session.destroy();
return next(err);
}
});
}
/**
* Logout.
* @param {Object} req the request
* @param {Object} res the response
* @param {Function} next the next middleware
*/
function logout(req, res, next) {
req.session.user = {};
res.end();
}
/**
* save profile
*
*/
function saveProfile(req, res, next) {
var profile = req.body;
var profileId = req.params.profileId;
var username = req.body.authInfo.username;
async.waterfall([
function (cb) {
MemberService.validateprofile(profile, cb);
}, function (profile, cb) {
MemberService.saveProfile(profile, profileId, cb);
},
function(result) {
res.json(helper.buildResponse({
profileId: profileId,
message: result
}));
}], function (err) {
if (err) {
req.session.destroy();
return next(err);
}
});
}
/**
* get profile
*
*/
function getProfile(req, res, next) {
var profileId = req.params.profileId;
var username = req.body.authInfo.username;
async.waterfall([
function (cb) {
MemberService.getProfile(profileId, cb);
},
function(result) {
res.json(helper.buildResponse(result));
}], function (err) {
if (err) {
req.session.destroy();
return next(err);
}
});
}
/**
* delete profile
*
*/
function deleteProfile(req, res, next) {
var profileId = req.params.profileId;
async.waterfall([
function (cb) {
MemberService.deleteProfile(profileId, cb);
},
function(result) {
res.json(helper.buildResponse(result));
}], function (err) {
if (err) {
req.session.destroy();
return next(err);
}
});
}
module.exports = {
login: login,
register:register,
logout: logout,
saveProfile:saveProfile,
getProfile:getProfile,
deleteProfile:deleteProfile
};
Add fs to app.js
var fs = require('fs')
1
app.use(logger('dev'));
1
Now if we run from the browser we can see that every request is being
logged.
0.397 ms - -
ExpressJS App:
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
notifier.notify({
title: title,
message: str
})
}
Install formidable.
npm install --save formidable
form.parse(req);
<!DOCTYPE html>
<html>
<head>
<title>Simple File Upload Example</title>
</head>
<body>
<form action="/" enctype="multipart/form-data" method="post">
<input type="file" name="upload" multiple>
<input type="submit" value="Upload">
</form>
</body>
</html>
jwt = require("express-jwt");
app.use(function(req, res, next) {
var token = req.body.token || req.query.token || req.headers['x-access-
token'];
if (token) {
jwt.verify(token, app.get('superSecret'), function(err, decoded) {
if (err) {
return res.json({ success: false, message: 'Invalid token.' });
} else {
req.decoded = decoded;
next();
}
});
} else {
return res.status(403).send({
success: false,
message: 'No token given.'
});
}
});