Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var express = require('express'); // Web Framework
- var bodyParser = require('body-parser');
- var sql = require('mssql'); // MS Sql Server client
- var app = express();
- var router = express.Router();
- //Body Parser
- app.use(bodyParser.urlencoded({ extended: false }));
- app.use(bodyParser.json({ type: 'application/json' }));
- //Route
- app.use("/api/images", router)
- //CORS Middleware
- router.use(function (req, res, next) {
- //Enabling CORS
- res.header("Access-Control-Allow-Origin", "*");
- res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
- res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, contentType,Content-Type, Accept, Authorization");
- next();
- });
- //Starting the server on port 8080
- var server = app.listen(process.env.PORT || 8080, function () {
- var port = server.address().port;
- console.log("App now running on port", port);
- });
- // Connection string parameters.
- var dbConfig = {
- user: 'sa',
- password: '********',
- server: 'localhost\\SQLEXPRESS01',
- database: 'ImageDB'
- };
- //Start connection pool
- var dbConn = new sql.ConnectionPool(dbConfig);
- //ExecuteQuery that is passed in.
- var executeQuery = function(res, query){
- sql.connect(dbConfig, function (err, dbConn) {
- if (err) {
- console.log("Error while connecting database :- " + err);
- res.send(err);
- sql.close();
- }
- else {
- // create Request object
- var request = new sql.Request(dbConn);
- // query to the database
- request.query(query, function (err, rs) {
- //handling errors
- if (err) {
- console.log("Error while querying database :- " + err);
- sql.close();
- res.send(err);
- }
- else {
- //close connection
- sql.close();
- // Prepare the response
- res.writeHead(200, {'Content-Type': 'application/json'});
- //Create an array and push query results to it
- var images = [];
- for (var i = 0; i < rs.recordset.length; i++) {
- images.push({id: rs.recordset[i].ID.toString(), image : new Buffer(rs.recordset[i].Image).toString("base64")});
- };
- res.end(JSON.stringify({ images }));
- }
- });
- }
- });
- }
- //GET API
- router.get("/:id", function(req , res){
- var id = req.params.id;
- var query = "select * from [Images] where ID=" + id;
- executeQuery (res, query);
- });
- module.export = router;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement