<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sagnik Chakraborti on Medium]]></title>
        <description><![CDATA[Stories by Sagnik Chakraborti on Medium]]></description>
        <link>https://medium.com/@nickborti?source=rss-d4cb91aae156------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*MOz35p2S5RNj7J0gmXk6eA.jpeg</url>
            <title>Stories by Sagnik Chakraborti on Medium</title>
            <link>https://medium.com/@nickborti?source=rss-d4cb91aae156------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 10:09:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nickborti/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Removing the Ugly Hash From AngularJS URL]]></title>
            <link>https://medium.com/@nickborti/removing-the-ugly-hash-from-angularjs-url-8823aa65a052?source=rss-d4cb91aae156------2</link>
            <guid isPermaLink="false">https://medium.com/p/8823aa65a052</guid>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Sagnik Chakraborti]]></dc:creator>
            <pubDate>Fri, 14 Oct 2016 17:36:10 GMT</pubDate>
            <atom:updated>2016-10-14T17:36:10.595Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>Article originally posted <a href="https://www.nickborti.com/blog/post/removing-the-ugly-hash-from-angularjs-url">here</a></blockquote><p>AngularJS by default provides a URL with a hash “#” in between. It’s very ugly to look at and will create problems in Search Engine Optimization. Google won’t like it. It’s irony that they have created AngularJS.</p><p>Let’s remove the hash and make it work. It’s very simple.</p><p>Suppose your app name is the traditional “myApp” and you have two views.</p><pre>var app = angular.module(&quot;myApp&quot;, [&#39;ngRoute&#39;]);<br>app.config([ &#39;$routeProvider&#39;, &#39;$locationProvider&#39;, function($routeProvider, $locationProvider){</pre><pre>$routeProvider<br>      .when(&#39;/test1&#39;, {<br>        controller: &#39;Test1Controller&#39;,<br>        templateUrl: &#39;views/test1.html&#39;<br>      })<br>      .when(&#39;/test2&#39;, {<br>        controller: &#39;Test2Controller&#39;,<br>        templateUrl: &#39;views/test2.html&#39;<br>      })<br>     .when(&#39;/&#39;, {<br>        controller: &#39;MainController&#39;,<br>        templateUrl: &#39;views/test.html&#39;<br>      })<br>   }]);</pre><p>So, here we have 2 views each one of them has a controller. The <strong>$locationProvider</strong> that we are passing in config as a paremeter will do the work of removing the hash. All you need to do is to add <strong>$locationProvider.html5Mode(true);</strong> before the closing braces of the config and after the <strong>$routeProvider</strong>.</p><pre>app.config([ &#39;$routeProvider&#39;, &#39;$locationProvider&#39;, function($routeProvider, $locationProvider){</pre><pre>$routeProvider<br>      .when(&#39;/test1&#39;, {<br>       ........<br>      })<br>      .when(&#39;/test2&#39;, {<br>      .......<br>      })</pre><pre>$locationProvider.html5Mode(true);<br>}]);</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/367/0*yIxoeqQa5GD-YFNM.jpg" /></figure><p>This is the AngularJS part. However, we have to make some changes in our main html file as well. Let’s say the name of our file is index.html</p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html lang=&quot;en-US&quot; ng-app=&quot;myApp&quot; ng-controller=&quot;MainController&quot;&gt;<br>&lt;head&gt;<br>   &lt;meta charset=&quot;utf-8&quot;&gt;<br>   &lt;base href=&quot;/&quot; /&gt;<br>&lt;/head&gt;<br>&lt;body&gt;</pre><pre>  &lt;a href=&quot;test1&quot;&gt;Test 1&lt;/a&gt;<br>  &lt;a href=&quot;test2&quot;&gt;Test 2&lt;/a&gt;<br>  <br>  &lt;ng-view&gt;&lt;/ng-view&gt;</pre><pre>&lt;/body&gt;<br>&lt;/html&gt;</pre><p>All we have to do is to add <strong>&lt;base href=”/” /&gt;</strong> in our head section. The href will contain the root directory path. If your app is inside a folder in the root directory then replace the slash with the name of your directory. So it will look like <strong>&lt;base href=”myAppFolder” /&gt;</strong></p><p>In previous scenarious we had to add # in our page view links. like <strong>&lt;a href=”#/test1&quot;&gt;Test&lt;/a&gt;</strong>. You don’t need that omit that in this case. Job Done!</p><p>But Not yet!!. If you go to the url directly like <a href="http://localhost/test1"><strong>http://localhost/test1</strong></a> and hit Enter, it will give error. This is because your server does not know the routing changes you have made in Angular. Hoping that your server is Apache, here is the .htaccess file for your configuration</p><pre>RewriteEngine   On<br>RewriteBase     /<br>RewriteCond     %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)<br>RewriteCond     %{REQUEST_FILENAME} !-f<br>RewriteCond     %{REQUEST_FILENAME} !-d<br>RewriteRule     ./index.html [L]</pre><p>Restart the server and you will see changes!</p><p>However, if you are using Express as a server instead of Apache or Nginx, then write this line of code in your server.js file at the end of all routes.</p><pre>app.all(&#39;*&#39;, function(req, res, next) {<br>    // Just send the index.html for other files to support HTML5Mode<br>     res.sendFile(__dirname + &quot;/public/index.html&quot;); // updated to reflect dir structure</pre><pre>});</pre><p>And in your index.html</p><pre>&lt;base href=&quot;index.html&quot; /&gt;</pre><blockquote><em>Resources:</em></blockquote><blockquote><a href="https://docs.angularjs.org/api/ng/provider/$locationProvider"><em>Angular Docs</em></a></blockquote><blockquote><a href="http://stackoverflow.com/questions/13832529/how-to-config-routeprovider-and-locationprovider-in-angularjs"><em>http://stackoverflow.com/questions/13832529/how-to-config-routeprovider-and-locationprovider-in-angularjs</em></a></blockquote><blockquote><a href="https://github.com/angular-ui/ui-router/issues/2032"><em>https://github.com/angular-ui/ui-router/issues/2032</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8823aa65a052" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ajax Form Submission along with Image Upload in Nodejs with Multer]]></title>
            <link>https://medium.com/@nickborti/ajax-form-submission-along-with-image-upload-in-nodejs-with-multer-174dbada81a6?source=rss-d4cb91aae156------2</link>
            <guid isPermaLink="false">https://medium.com/p/174dbada81a6</guid>
            <category><![CDATA[expressjs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[nodejs]]></category>
            <dc:creator><![CDATA[Sagnik Chakraborti]]></dc:creator>
            <pubDate>Fri, 14 Oct 2016 08:39:09 GMT</pubDate>
            <atom:updated>2016-10-14T08:39:09.388Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Article originally posted </em><a href="https://www.nickborti.com/blog/post/ajax-form-submission-along-with-image-upload-in-nodejs-with-multer"><em>here</em></a></blockquote><p><a href="https://www.npmjs.com/package/multer">Multer</a> has changed the way it uploads file since it released version 1.0.0, I guess. Hence it’s a bit confusing to figure out which is the right way to use it. I came across this difficulty and thought I would write a blog about it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/381/0*dZqdqCX-1xBklNnV.jpg" /></figure><p>Back in 2015, I remember, I started using Multer and the concept was to submit a form with data and uploading an image along with it. Here’s the code what I wrote back then on the backend.</p><pre>var multer = require(&#39;multer&#39;);<br>var upload = multer({dest: &#39;./public/images/uploads/&#39;});</pre><pre>router.post(&#39;/add&#39;, upload.single(&#39;imagename&#39;), function(req, res, next) {</pre><pre>var title = req.body.title;<br>var body = req.body.body;<br>var image = req.file.filename;</pre><pre>/*  rest of the work  */</pre><pre>});</pre><p>Certainly, this worked like a charm back then. I forgot about it and moved on with other things. In a recent project in Nodejs, I had to write more or less the same thing like before. This time though, I was clueless. It didn’t work at first. Then after some tweaks, it uploaded the image, but without any extension! How’s that for a comeback?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/573/0*w_Kee7t3QVRiXKan.jpg" /></figure><p>I wondered “what the hell”? After googling a lot, from forums here and there , I found out that they have removed the file extension and it has become independent since version 1.0.0 . In the current circumstances we have to change the way we wrote the code a bit. The multer middleware gives us the freedom to set the destination, filename and many other objects. Here’s a quick example.</p><pre>var multer = require(&quot;multer&quot;);<br>var storage = multer.diskStorage({<br>    destination: function(req, file, callback){<br>        callback(null, &#39;./public/uploads&#39;); // set the destination<br>    },<br>    filename: function(req, file, callback){<br>        callback(null, Date.now() + &#39;.jpg&#39;); // set the file name and extension<br>    }<br>});</pre><pre>var upload = multer({storage: storage});</pre><pre>router.post(&#39;/add&#39;, upload.single(&#39;imagename&#39;), function(req, res, next) {<br>    var image = req.file.filename;<br>   /** rest */ <br>});</pre><p>This is a basic example of how you can use the multer middleware to upload an image. This can be vulnerable to security. But here’s just the heads up. A simple <strong>multipart/form-data</strong> form submit with the image name as <strong>imagename</strong> can get your job done. But since I don’t get satisfied quickly, I wanted to do that with Ajax.</p><p>It’s quite simple. But here’s what I did. I used jquery-form for the ajax submission part.</p><pre>&lt;form class=&quot;uploadImage&quot; enctype=&quot;multipart/form-data&quot; action=&quot;/add&quot; method=&quot;POST&quot;&gt;<br>   &lt;input type=&quot;text&quot; id=&quot;title&quot;&gt;<br>   &lt;input type=&quot;file&quot; name=&quot;imagename&quot; id=&quot;imagename&quot;&gt;<br>   &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;&gt;<br>&lt;/form&gt;</pre><pre>&lt;script src=&quot;//ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js&quot;&gt;&lt;/script&gt;<br>&lt;script src=&quot;//cdnjs.cloudflare.com/ajax/libs/jquery.form/3.51/jquery.form.min.js&quot;&gt;&lt;/script&gt;</pre><pre>&lt;script&gt;<br>   $(&#39;.uploadImage&#39;).submit(function(e){<br>     e.preventDefault();<br>     <br>     var title = $(&#39;#title&#39;).val(); <br>     <br>     $(this).ajaxSubmit({<br>       data: {title: title},<br>       contentType: &#39;application/json&#39;,<br>       success: function(response){<br>         console.log(&#39;image uploaded and form submitted&#39;);     <br>       }<br>   });<br>     return false;<br>});<br>&lt;/script&gt;</pre><p>You can get the title on the routes with <strong>req.body.title</strong>. If you have to pass other fields as well, just add them up in the data object in ajax and receive them on the server end. While you get your fields’ data you can upload the image by injecting the multer middleware in the route. And since you have the access to the filename and you can set one, you can insert that into database as well!</p><blockquote><em>Some resources:</em></blockquote><blockquote><a href="https://github.com/expressjs/multer/issues/170"><em>Github multer issues</em></a></blockquote><blockquote><a href="https://ewiggin.gitbooks.io/expressjs-middleware/content/multer.html"><em>https://ewiggin.gitbooks.io/expressjs-middleware/content/multer.html</em></a></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=174dbada81a6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>