Over a million developers have joined DZone.

More on my JavaScriptCookbook Node project

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

Over the past couple of days I've made more progress on the Node project I started. If you haven't read the first article, click that previous link for the background. Here's what I've been able to do since my first post.

GitHub

I mentioned this in the comments, but if you want to look at, and criticize, my Node code, you can do so at GitHub: https://github.com/cfjedimaster/javascriptcookbook. Every thing is in there except for the JSON file that I used to store authentication information for the admin panel and my GMail integration (more on that in a minute). As I think I say every time, I'm still a Node/Express noob, so I wouldn't consider this to be good code, but the site is complete for now.

Email

I used the excellent Nodemailer module to setup email support for my application. All I needed was the ability to send an email to myself and this worked fine. Here's a code snippet from my app.js showing this in action.
  var title = req.param('title');
	var body = req.param('body');
	var code = req.param('code');
	var sourceauthor = req.param('sourceauthor');
	var sourceurl = req.param('sourceurl');
	var yourname = req.param('yourname');
	var youremail = req.param('youremail')
	var tags = req.param('tags');

  var transport = nodemailer.createTransport("SMTP", {
		service: 'Gmail', // use well known service
			auth: {
				user: app.get('mailusername'),
				pass: app.get('mailpassword')
			}
	});

  var message = {
	
		// sender info
		from: '"' + yourname +'" <' + youremail +'>',
	
		// Comma separated list of recipients
		to: '"Raymond Camden" <raymondcamden@gmail.com>',
	
		// Subject of the message
		subject: 'JavaScript Cookbook Submission', //
	
		text: "Title: "+title + "\n" +
		"Body: "+body + "\n\n" + 
		"Code: "+code + "\n\n" + 
		"Source Author: " + sourceauthor + "\n" +
		"Source URL: " + sourceurl + "\n" + 
		"Submitter Name: " + yourname + "\n" + 
		"Submitter Email: " + youremail + "\n"
	
	};	

	transport.sendMail(message, function(error){
		if(error){
			console.log('Error occured');
			console.log(error.message);
			return;
		}
		console.log('Message sent successfully!');

		// if you don't want to use this transport object anymore, uncomment following line
		transport.close(); // close the connection pool
		
		res.redirect('/submitted');
	});

The only oddity I ran into was that even though I set the From to be your name and email address, when it shows up in Gmail it is always my email address. I'm assuming that is a GMail security thing. If anyone knows better, let me know. Since I include the sender's email address anyway this isn't a deal breaker for me.

MongoDB

I tell ya what. I never want to write SQL again. This isn't the first time I used Mongo but my god - what a pleasure. Here are a few examples.
//get latest
article_collection.find().sort({created_at:-1}).limit(max).toArray(function(error, results) {
			if( error ) callback(error)
			else callback(null, results)
});

//based on an array property called tags, give me unique ones
article_collection.distinct("tags", function(error, result) {
		if(error) callback(error);
		else callback(null, result);
});

//loose search
var reg = new RegExp(term,"i");
article_collection.find({$or:[{title: reg},{body:reg}]}).toArray(function(error, results) {				
		if( error ) callback(error)
		else callback(null, results)
});

I think that $or search is the one I like the most.

Templating

I hate the Jade templating system and EJS is ok, but my favorite templating system is Handlebars. The HBS module gives me access to that inside my views. I can even extend it with my own utility functions. Here is one sample view:
<h3>{{article.title}}</h3>

<p>
{{article.body}}
</p>

{{#if article.sourceauthor}}
Original Author: {{article.sourceauthor}}<br/>
{{/if}}

{{#if article.sourceurl}}
Original Location: <a href="{{article.sourceurl}}">{{article.sourceurl}}</a><br/>
{{/if}}

<p/>

<pre>{{article.code}}</pre>


<p>
{{#if article.tags}}
<b>Tags:</b>
  {{#each article.tags}}
	<a href="/tag/{{urlFormat .}}"><span class="label label-info">{{.}}</span></a>
	{{/each}}
{{/if}}
</p>

Commenting

Yeah, Disqus. Done.

Hosting

I love AppFog. How much do I love AppFog? AppFog is this:

Let me describe what their process was like for me.

  • I signed up.
  • I made a new app.
  • I clicked about 2 buttons to add Mongo support.
  • I installed the command line tool via npm.
  • I typed "af update javascriptcookbook"

That's it. I was done. Period. And it worked! Now it turns out I was supposed to modify one line: app.listen(3000) needed to change to app.listen(process.env.VCAP_APP_PORT || 3000). But even before I did that it worked. Heck, I didn't even have to tweak Mongo.

So now my process is - I make some changes - I commit to GitHub - I push up to AppFog. The update process for AppFog takes about 20 seconds.

I'm so happy with them that I'm going to ahead and take the plunge and spend the 20 bucks a month. (Although I'll probably end up putting an Adsense ad on the site to help pay for it.)

Want to give it a whirl? Check it out here: http://javascriptcookbook.aws.af.cm. I am definitely looking for some submissions now and would love someone to make a favicon if they feel so inclined. ;)

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.

Topics:

Published at DZone with permission of Raymond Camden, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}