Showing posts with label nodejs. Show all posts
Showing posts with label nodejs. Show all posts

Standalone/Non-NodeJS version of a simple Single Page Application ( SPA ) with React Router

Project Structure:

./libs/jquery/2.1.1/jquery.min.js
./libs/react/react-router/3.0.0/react-router.js
./libs/react/0.14.2/react-dom.js
./libs/react/0.14.2/react.js
./libs/babel/babel-core/5.8.23/browser.min.js
./libs/history/3.0.0/history.js
./index.html


index.html
========

<!-- index.html -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Antwan Wimberly's React Router Tutorial via ES5 and Babel</title>
    <script src="libs/react/0.14.2/react.js"></script>
    <script src="libs/react/0.14.2/react-dom.js"></script>
    <script src="libs/babel/babel-core/5.8.23/browser.min.js"></script>
    <script src="libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="libs/react/react-router/3.0.0/react-router.js"></script>
    <script src="libs/history/3.0.0/history.js"></script>
    <style>
    body {
  padding: 50px;
  font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
}

a {
  color: #00B7FF;
}
    </style>
  </head>
  <body>
    <div id="content"></div>
    <script type="text/babel">
      // To get started with this tutorial running your own code, simply remove
      // the script tag loading scripts/example.js and start writing code here.
      // make these convenietly available so we don't have to fully qualify 
them (i.e Router vs ReactRouter.Router)
      var Router = ReactRouter.Router;
      var Link = ReactRouter.Link; // instead of straight up <a href="blah">
 we'll have to use the Link component instead
      var Route = ReactRouter.Route;
      var history = History.useBasename(History.createHistory)();
      var IndexPage = React.createClass({
        render: function() {
          return (
            // render the content from the child route via this.props.children
 (i.e. DetailsPage, Users, User)
            <div className="indexPage">
              Hello, world! I am a index page.
              <ul>
                <li><Link to="/details">Details</Link></li>
                <li><Link to="/users">Users</Link></li>
                <li><Link to="/stand-alone">Stand Alone</Link></li>
              </ul>
              {this.props.children}
            </div>
          );
        }
      });
      var DetailsPage = React.createClass({
        render: function() {
          return (
            <div className="detailsPage">
              Hello, world! I am a details page.
            </div>
          );
        }
      });
      var NotFound = React.createClass({
        render: function() {
          return (
            <div className="no-match">
              Woah dere!!! 404 Not Found .Fix that!!!
            </div>
          );
        }
      });
      var Users = React.createClass({
        render: function() {
          return (
            <div id="users">
              This is just the users shell, bra!!!
              <div><Link to="/users/user/1">See User #1</Link></div>
              My child content for the Users compoent is.....
              {this.props.children}
            </div>
          );
        }
      });
      var User = React.createClass({
        render: function() {
          return (
            <div id="user-{this.props.params.userId}">
              The user you are retrieving details for is {this.props.params.userId}
            </div>
          );
        }
      });
      var NonNestedPage = React.createClass({
        render: function() {
          return (
            <p>Hi, I do not share content with the IndexPage component.
 I am stand alone</p>
          );
        }
      });
      ReactDOM.render(
        <Router history={history}>
          <Route path="/" component={IndexPage}>
            <Route path="details" component={DetailsPage} />
            <Route path="users" component={Users}>
              <Route path="user/:userId" component={User} />
            </Route>
          </Route>
          <Route path="/stand-alone" component={NonNestedPage} />
          <Route path="*" component={NotFound} />
        </Router>,
        document.getElementById('content')
      );
    </script>
  </body>
</html>


-------------------------

Full working code Download .


For running refer to  https://mohiplanet.blogspot.com/2015/10/react-bootstrap-listgrid-toggle.html

References:
https://github.com/armw4/react-router-example-es5

React Native : Going fullscreen on Android

Untitled

styles.xml:

in android/app/src/main/res/values/styles.xml configure like following:

AndroidManifest.xml:

in android/app/src/main/AndroidManifest.xml configure like following:

Hiding Navigation Bar:

For making navigation go away completely simply put navigation bar into immersive sticky mode by putting MainActivity.java. In android/app/src/main/java/com/company/MainActivity.java like following:
Now navigation bar can only be accessed by tapping near at only the edge of screen.
References:
https://facebook.github.io/react-native/docs/statusbar.html
https://developer.android.com/jetpack/androidx/releases/appcompat

uninstall nodejs completely and install old lagacy version 0.8.23 script on nodejs

  1. which node
  2. # should output /usr/bin/node
  3. cd /usr/bin
  4. rm -rf bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1
  5. rm -f /usr/bin/node
  6. #remove npm
  7. npm rm npm -g
  8. rm -rf ~/.npm
  9. cd /opt/
  10. #download nodejs version 0.8.23
  11. wget http://nodejs.org/dist/v0.8.23/node-v0.8.23.tar.gz
  12. #extract
  13. tar -xvf node-v0.8.23.tar.gz
  14. rm -f node-v0.8.23.tar.gz
  15. cd node-v0.8.23
  16. ./configure
  17. #build and install
  18. make
  19. make install
  20. #create symlinks
  21. ln -s /usr/local/bin/node /usr/bin/node
  22. #check version
  23. node -v

Parsing xml response data with Nodejs

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ns2:urlset xmlns:ns2="http://www.sitemaps.org/schemas/sitemap/0.9" hc="a3">
  3. <url>
  4. <loc>http://www.flipclub.com/</loc>
  5. <lastmod>2015-06-09</lastmod>
  6. <changefreq>daily</changefreq>
  7. <priority>1.0</priority>
  8. </url>
  9. <url>
  10. <loc>http://www.flipclub.com/flashcard/46/3rd-Grade:-Ancient-Rome-Flashcards</loc>
  11. <lastmod>2015-06-09</lastmod>
  12. <changefreq>daily</changefreq>
  13. <priority>1.0</priority>
  14. </url>
  15. <url>
  16. <loc>http://www.flipclub.com/flashcard/47/7th-Grade:-Social-Studies-Flashcards</loc>
  17. <lastmod>2015-06-09</lastmod>
  18. <changefreq>daily</changefreq>
  19. <priority>1.0</priority>
  20. </url>
  21. <url>
  22. <loc>http://www.flipclub.com/flashcard/1068/ACT-English-Game-Set-2-Flashcards</loc>
  23. <lastmod>2015-06-09</lastmod>
  24. <changefreq>daily</changefreq>
  25. <priority>1.0</priority>
  26. </url>
  27. <url>
  28. <loc>http://www.flipclub.com/flashcard/1070/ACT-Exam-Math-Game-Set-1-Flashcards</loc>
  29. <lastmod>2015-06-09</lastmod>
  30. <changefreq>daily</changefreq>
  31. <priority>1.0</priority>
  32. </url>
  33. <url>
  34. <loc>http://www.flipclub.com/flashcard/1069/ACT-Exam-Science-Game-Set-1-Flashcards</loc>
  35. <lastmod>2015-06-09</lastmod>
  36. <changefreq>daily</changefreq>
  37. <priority>1.0</priority>
  38. </url>
  39. </ns2:urlset>
Sample Parsed Output:-

http://flipclub.com/
http://flipclub.com/flashcard/46/3rd-Grade:-Ancient-Rome-Flashcards
http://flipclub.com/flashcard/47/7th-Grade:-Social-Studies-Flashcards
http://flipclub.com/flashcard/1068/ACT-English-Game-Set-2-Flashcards
http://flipclub.com/flashcard/1070/ACT-Exam-Math-Game-Set-1-Flashcards
http://flipclub.com/flashcard/1069/ACT-Exam-Science-Game-Set-1-Flashcards

Codes:-
  1. var url = "http://flipclub.com/sitemap.xml";//get xml from sample sitemap.xml
  2. var http = require("http");
  3. var parseString = require('xml2js').parseString;//make sure you have installed xml2js with "npm install xml2js"
  4. var req = http.get(url, function(res) {
  5. // save the data
  6. var xml = '';
  7. res.on('data', function(chunk) {
  8. xml += chunk;
  9. });
  10. res.on('end', function() {
  11. parseString(xml, function(err, result) {
  12. var items = result['ns2:urlset']['url'];
  13. items.forEach(function(entry) {
  14. console.log(entry['loc'][0]);
  15. });
  16. });
  17. });
  18. });
  19. req.on('error', function(err) {
  20. // debug
  21. console.log(err);
  22. });

Making javascript heavy sites crawlable / SEO with Prerender.IO / Getting started with Prerender.io for java sites

Setup prerender node server:
  1. git clone https://github.com/prerender/prerender.git
  2. cd prerender/
  3. #build and install
  4. # you will need nodejs's npm utility
  5. npm install
  6. node server.js

Indicate to the crawler that your site supports the AJAX crawling scheme :

replace all hash("#") with hash-bang("#!")

To handle pages without hash fragments put this in those pages
<meta name="fragment" content="!">

pom.xml :
//add this survlet filter( https://github.com/greengerong/prerender-java ) in your pom.xml:

  1. <dependency>
  2. <groupId>com.github.greengerong</groupId>
  3. <artifactId>prerender-java</artifactId>
  4. <version>1.6.2</version>
  5. </dependency>

web.xml :

  1. <filter>
  2. <filter-name>prerender</filter-name>
  3. <filter-class>com.github.greengerong.PreRenderSEOFilter</filter-class>
  4. <init-param>
  5. <param-name>prerenderServiceUrl</param-name>
  6. <param-value>http://localhost:3000</param-value>
  7. </init-param>
  8. <init-param>
  9. <param-name>crawlerUserAgents</param-name>
  10. <param-value>yourOwnCrawler</param-value>
  11. </init-param>
  12. <init-param>
  13. <param-name>extensionsToIgnore</param-name>
  14. <param-value>.xml</param-value>
  15. </init-param>
  16. </filter>
  17. <!--default crawlers "googlebot", "yahoo", "bingbot", "baiduspider",
  18. "facebookexternalhit", "twitterbot", "rogerbot", "linkedinbot", "embedly" -->
  19. <filter-mapping>
  20. <filter-name>prerender</filter-name>
  21. <url-pattern>/*</url-pattern>
  22. </filter-mapping>


//prerender-java client bug 1.6.2
you have to include this tag
<init-param>
            <param-name>extensionsToIgnore</param-name>
            <param-value>.xml</param-value>
        </init-param>
//for 1.6.3 this is not required, but it's not yet in central maven repository

all Other clients list :
https://github.com/prerender/prerender#middleware

Testing:

git clone https://github.com/ahmedmohiduet/ng-prerender-demo.gitcd ng-prerender-demo
mvn clean install tomcat7:run


curl -A "GoogleBot" http://localhost:9966/ng-prerender-demo/
#don't forget to put / at the end
curl http://10.0.0.52:9966/ng-prerender-demo/
#note the difference

you can also show them sample content:
http://localhost:3000/http://www.flipclub.com/card/422/Archaeology-1 
or
http://service.prerender.io/http://www.flipclub.com/card/422/Archaeology-1 

You can also use service : http://service.prerender.io/


Prerender caches :

* In-memory html cache (just uncomment this line at https://github.com/prerender/prerender/blob/master/server.js#L18 and in-memory cache will b enabled.)
* Amazon s3
* Redis Cache
* Your Own Implementation ( Example : https://github.com/lammertw/prerender-mongodb-cache )
* More.*..

It's paid version provides automatic caching of pages and persistent cache automatically gets synchronized. 

Install/Configure and run HTML pre-render service on CentOS

To serve javascript rendered page as html page  to search engine crawlers so that our site becomes SEO friendly, we can use node server like prerender.We configure and run prerender server by using this .sh script:
    1. #install nodejs(Run "yum clean all && rpm --rebuilddb && yum update" in case yum cant find them)
    2. yum install nodejs
    3. #install nodejs,s npm utility
    4. yum install npm
    5. #install Font configuration and customization library
    6. yum install fontconfig-2.8.0-3.el6.i686
    7. cd /usr/local/
    8. #download prerender server
    9. git clone https://github.com/collectiveip/prerender.git
    10. cd prerender/
    11. #build and install
    12. npm install
    13. #create runner and log file
    14. echo "node server.js >> \"prerender_log.txt\"" >> prerender_runner.sh
    15. #set to run at startup
    16. echo "nohup /usr/local/prerender/prerender_runner.sh &" >> /etc/rc.local
    17. #start prerender
    18. sh prerender_runner.sh &
    19. #check output from prerender server
    20. tail -f prerender_log.txt
Do all this by using this Script to download,configure & run prerender server.
By default it starts on port 3000.So you will find it at : localhost:3000
To test if it successfully installed simply browse to this url:
http://localhost:3000/http://www.facebook.com

Right click on document and click view page source.You won't find any javascript there cause the page comes after all javascripts are executed.
Be careful about url syntax.Omitting protocol(http/https) and www will not work.Like these urls will not work:
http://localhost:3000/www.facebook.com
http://localhost:3000/http://facebook.com


Related:-
Making javascript heavy sites crawlable / SEO with Prerender.IO / Getting started with Prerender.io for java sites
Running web application & prerender node server on same machine