• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
nodejs tips and tricks

#11
(09-12-2018, 04:04 AM)ignitedcms Wrote: Thank you for your reply skunkbad. Sorry for my ignorance, but could you clarify what you mean by Angular, react and vue. I see these as front end frameworks whereas nodejs is mainly backend stuff.

I too am I bit fussy about the way nodejs acquires modules or middleware, actually this was one reason why I always disliked fragmented frameworks where you are assumed to acquire different modules via composer. I guess this is just how nodejs operates, so you are at the mercy of the repo author I guess.

Additionally, I know this might be a bit old school, but I feel quite content with jquery, and a few additional plugins for all my front end work. I didn't fancy investing the time to learn angular etc but I understand it is hot on the skills requirements these days.

These front end frameworks are all available through node, and same with Gulp, Grunt, Webpack, Sass, etc, etc. If you're going to use node on the server only, then I guess what I wrote wouldn't be of use. Personally, I don't have enough experience to see how replacing a typical LAMP stack with node + whatever has any real benefits. I'd be interested to know these benefits if you care to explain. I for one don't see how JS on the server (with 1000+ packages) can be as easy and powerful as PHP, and PHP is super well documented, super stable, and all in one place.

Things have totally changed over the years, and even with Composer it's likely to be pulling in a lot of dependencies that end up in ones app. I just think with everything I've done through npm it is more than a little overwhelming.

I guess the theory is that the way jQuery works is obsolete if you intend to have a sophisticated SPA (single page app). I've made some very complex SPAs with jQuery, and even with the best organization they get a bit messy. My SPAs are nothing compared to the interface of gmail (as an example), and so when I think about doing gmail with jQuery it seems the wrong tool. That's where these new front end frameworks come in. In the case of React, it's also claiming big performance benefits compared to jQuery.

I use jQuery every day, so I can't really bash it, and I'll admit that I'm not a guru level JS dev, and I also am not sure that there are any real advantages of using it for the kind of work I usually do. I'm just saying that I'm willing to step outside that box in order to make money, as this is my job, and I'm not here just to play around.
Reply

#12
(09-12-2018, 08:54 AM)skunkbad Wrote:
(09-12-2018, 04:04 AM)ignitedcms Wrote: Thank you for your reply skunkbad. Sorry for my ignorance, but could you clarify what you mean by Angular, react and vue. I see these as front end frameworks whereas nodejs is mainly backend stuff.

I too am I bit fussy about the way nodejs acquires modules or middleware, actually this was one reason why I always disliked fragmented frameworks where you are assumed to acquire different modules via composer. I guess this is just how nodejs operates, so you are at the mercy of the repo author I guess.

Additionally, I know this might be a bit old school, but I feel quite content with jquery, and a few additional plugins for all my front end work. I didn't fancy investing the time to learn angular etc but I understand it is hot on the skills requirements these days.

These front end frameworks are all available through node, and same with Gulp, Grunt, Webpack, Sass, etc, etc. If you're going to use node on the server only, then I guess what I wrote wouldn't be of use. Personally, I don't have enough experience to see how replacing a typical LAMP stack with node + whatever has any real benefits. I'd be interested to know these benefits if you care to explain. I for one don't see how JS on the server (with 1000+ packages) can be as easy and powerful as PHP, and PHP is super well documented, super stable, and all in one place.

Things have totally changed over the years, and even with Composer it's likely to be pulling in a lot of dependencies that end up in ones app. I just think with everything I've done through npm it is more than a little overwhelming.

I guess the theory is that the way jQuery works is obsolete if you intend to have a sophisticated SPA (single page app). I've made some very complex SPAs with jQuery, and even with the best organization they get a bit messy. My SPAs are nothing compared to the interface of gmail (as an example), and so when I think about doing gmail with jQuery it seems the wrong tool. That's where these new front end frameworks come in. In the case of React, it's also claiming big performance benefits compared to jQuery.

I use jQuery every day, so I can't really bash it, and I'll admit that I'm not a guru level JS dev, and I also am not sure that there are any real advantages of using it for the kind of work I usually do. I'm just saying that I'm willing to step outside that box in order to make money, as this is my job, and I'm not here just to play around.

Hi skunkbad, I guess the real benefits of nodejs is performance. If you are building a single site then I guess that is not of importance.

Nodejs basically replaces the LAMP stack, there is no php or apache, it is literally a process running in the background - it supports concurrency, so you will have lightening fast http requests, and db queries.

As websites demand increasingly more users etc, it is not sufficient to simply add more hardware, this is why people are typically considering nodejs and golang as their new web development envs.

Like I said, I'm just learning this now, so I can't really say, I'm just typing what I've already heard online.
Practical guide to IgnitedCMS - Book coming soon
Reply

#13
Additionally, yes I totally agree! Jquery is a real mess to organise especially if your front end is quite complex.

That being said, even something like angular doesn't have everything, it doesn't have drag and drop, which comes with jquery UI.

So even angular is fragmented somewhat if you want to do clever stuff on the front end!
Practical guide to IgnitedCMS - Book coming soon
Reply

#14
Right I'm just working my way through the tutorials and let me tell you, this gives you an appreciation for all the PHP functions and code igniter functions that we take for granted.

Even serving an image file took some time to figure out.

So far, I've successfully created a server, created routes for a html file that serves all content within. Parsed POST requests, still need to figure a way to send it back to the view, I think I might need handlebars?

Still need to install bcrypt, for password hashing, and the mysql npm to connect and talk to the database, joi for form validation, and either formidable or multr for file uploading and nodemailer for sending emails!

To kill the node process I need to use killall -9 node

Code:
'use strict';

const Hapi = require('hapi');

const server = Hapi.server({
    port: 3000,
    host: 'localhost'
});

server.route({
    method: 'GET',
    path: '/',
    handler: (request, h) => {

        return 'Hello, world!';
    }
});

server.route({
    method: 'GET',
    path: '/{name}',
    handler: (request, h) => {

        return 'Hello, ' + encodeURIComponent(request.params.name) + '!';
    }
});

const init = async () => {

    await server.register(require('inert'));
    await server.register(require('vision'));

    //serve any images
       server.route({
        method: 'GET',
        path: '/public/img/{param*}',
        handler: {
            directory: {
                path: 'public/img'
            }
        }
    });


    //post test
    server.route({
        method: 'POST',
        path: '/formsent',
        handler: (request, h) => {

            //get the post of name

            //const payload = request.payload
            //return payload.name

            //pass this back something back into the view
            //should I use handlebars?

            return h.redirect('hello',{title:'my home page'})
        }
    });


    server.route({
        method: 'GET',
        path: '/hello',
        handler: (request, h) => {

            return h.file('./public/hello.html');
        }
    });

    await server.start();
    console.log(`Server running at: ${server.info.uri}`);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();
Practical guide to IgnitedCMS - Book coming soon
Reply

#15
(09-17-2018, 03:20 PM)ignitedcms Wrote: ... this gives you an appreciation for all the PHP functions and code igniter functions that we take for granted.

This is why I started this: https://forum.codeigniter.com/thread-71482.html

Even you started this thread with, "I'm sure you must be aware of nodejs acquiring traction...". Only time will tell, but I have made quite a few LAMP based websites for customers in 2 or 3 hours, and I just don't know why I'd want to give that up. My average customer doesn't need a SPA, and I don't think the average person needs this node based website that the hipsters are cheerleading for.

I will say though, I've been learning more React/Redux in the last week, and I can see myself using it in my CodeIgniter projects instead of jQuery. I think it's going to be fun. I just dont see myself running node on the server and using Mongo and the like.

In time, I think what we're going to see is something like React that becomes super easy to use like jQuery. Those will be glorious days Smile
Reply

#16
(09-17-2018, 04:44 PM)skunkbad Wrote:
(09-17-2018, 03:20 PM)ignitedcms Wrote: ... this gives you an appreciation for all the PHP functions and code igniter functions that we take for granted.

This is why I started this: https://forum.codeigniter.com/thread-71482.html

Even you started this thread with, "I'm sure you must be aware of nodejs acquiring traction...". Only time will tell, but I have made quite a few LAMP based websites for customers in 2 or 3 hours, and I just don't know why I'd want to give that up. My average customer doesn't need a SPA, and I don't think the average person needs this node based website that the hipsters are cheerleading for.

I will say though, I've been learning more React/Redux in the last week, and I can see myself using it in my CodeIgniter projects instead of jQuery. I think it's going to be fun. I just dont see myself running node on the server and using Mongo and the like.

In time, I think what we're going to see is something like React that becomes super easy to use like jQuery. Those will be glorious days Smile

Very nice, personally, I'm throwing myself into vuejs as it seems the best off those three although, it is personal preference.

Yes it is true PHP is super quick, but let's not forgot it is still scripting / interpreted so on the slowest end of the languages. Node is concurrent, and built for large asynchronous apps. I'm actually going to take a long into sails.js after I crack hapi, it looks more like a MVC framework which is what drew me to CI.

Debugging is a bit tedious in node. But i can imagine big companies wanting to move toward this.
Practical guide to IgnitedCMS - Book coming soon
Reply

#17
After hours of trying I finally figured out how to use handlebars.

Code:
'use strict';

const Hapi = require('hapi');

const server = Hapi.server({
    port: 3000,
    host: 'localhost'
});

const init = async () => {

    await server.register(require('inert'));
    await server.register(require('vision'));

    //make sure folder templates is above js file and
    //contains index.html

    server.views({
        engines: {
            html: require('handlebars')
        },
        relativeTo: __dirname,
        path: './templates'
    });

    server.route({
        method: 'GET',
        path: '/',
        handler: function (request, h) {

            return h.view('index', { title: 'My home page' });
        }
    });
await server.start();
    console.log(`Server running at: ${server.info.uri}`);
};

process.on('unhandledRejection', (err) => {

    console.log(err);
    process.exit(1);
});

init();
Practical guide to IgnitedCMS - Book coming soon
Reply

#18
@skunkbad, I thought this was a good watch.

https://www.youtube.com/watch?v=DwQJ_NPQWWo
Practical guide to IgnitedCMS - Book coming soon
Reply

#19
(09-18-2018, 12:42 PM)ignitedcms Wrote: @skunkbad, I thought this was a good watch.

https://www.youtube.com/watch?v=DwQJ_NPQWWo

Yeah, I haven't used async + await yet.
Reply

#20
(09-18-2018, 03:05 PM)skunkbad Wrote:
(09-18-2018, 12:42 PM)ignitedcms Wrote: @skunkbad, I thought this was a good watch.

https://www.youtube.com/watch?v=DwQJ_NPQWWo

Yeah, I haven't used async + await yet.

I haven't used it either but this was the elevator pitch that initially drew me towards node.

ATM I'm just throwing together a lightweight framework, I checked out sails.js but it was monolithic, and a bit too confusing.

I've currently want to the following in node

-create a server
-call separate js files
-serve html and images
-route files
-connect to db
-escape queries
-pass data to views
-serverside validation
-ajax validation
-sessions
-hash passwords
-file uploads
-read write files
-socket server?

putting up a git repo
Practical guide to IgnitedCMS - Book coming soon
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
2 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.