Build a Hello World Website and Web Server with EJS, Node and Express
DON’T MISS OUR NEWEST KICKSTARTER: https://www.kickstarter.com/projects/johnbura/excel-machine-learning-and-blockchain-in-99-days?ref=38nryo
Hello World in EJS for Node JS Templating with HTML, JavaScript and Express
Also available at https://mammothinteractive.com/build-a-hello-world-website-and-web-server-with-ejs-node-and-express/
How to make your first EJS project
by Alexandra Kropova from Mammoth Interactive
Below are the steps to start templating with EJS, embedded JavaScript templates. Learn this essential language that every web developer must know.
Make a new Node.js project at Repl.com.
If you’re not using Repl.com, skip this step. You can use your computer’s built-in Terminal or Command Line application and a code editor.
What is Repl
You don’t have to use Repl. Repl makes it easier to use a terminal, code editor and server all at once in the browser. It’s free to make an account and use many programming languages.
Make a new project folder.
mkdir hello-ejs
Enter the folder
cd hello-ejs
Set up the npm project.
npm init -y
Install Express to help with making a server.
npm install express
Install EJS for templating.
npm install ejs
Build a server file in the project folder.
touch server.js (on Unix, Repl, Mac or Linux terminal)
copy con server.js (if using DOS Windows terminal)
Open the file in your online code editor or installed code editor. The file is empty by default. Set up the server file with the following code in the file.
var express = require(‘express’);
var app = express();
app.set(‘view engine’, ‘ejs’);
app.get(‘/’, function(req, res) {
res.render(‘pages/home’);
});
app.listen(8080);
Next, you have to make the page that is the response to the request of the user going to yourwebsite.com/ (the homepage). You must create the page you want to display as the homepage.
EJS expects a views folder to hold your pages (EJS views) and partials (reusable page sections.)
Build a views folder in your project folder.
mkdir views
Enter the folder.
cd views
Make a sub-folder to store your pages.
mkdir pages
Enter the folder.
cd pages
Make an EJS file to store the homepage.
touch home.ejs
copy con home.ejs (DOS Windows command line)
Open the file. Put in some code for the content of the homepage.
Notice here we referenced an EJS partial. We must make that partial.
Go a parent folder higher in the hierarchy.
cd ..
You should now be in the views folder. There, create a new folder to store your partials.
mkdir partials
Create a new file for your first partial.
It must be called ‘header’ because that is how we’ve already referenced it.
touch header.ejs
copy con header.ejs (if using Windows DOS command line)
Put some content into the file. The content should be header elements that you want to potentially reuse on multiple pages.
Go up to your project folder.
cd ..
cd ..
You should now be in hello-ejs.
Run the server with:
node server.js
Open the local host at port 8080 to see your website. You will see the partial appear on your homepage. You can reuse this partial on any page of the site.
💚 90% OFF ONLY ON YOUTUBE
● $4.99: HTML, CSS & JavaScript http://bit.ly/htmlcssjscourse
● $9.99 Level: Make 6 games in the Unreal Engine http://bit.ly/unrealcourse
● $29.99 Level: Build The Legend of Zenda Game http://bit.ly/zendaunity
● $39.99 Level: The Deep Learning Masterclass http://bit.ly/deeplearningkeras
● $49.99 Level: Build a Battle Royale! (50 Hours) http://bit.ly/howtobuildabattleroyale
● $59.99 Level: 2D and 3D Unity Games BUNDLE http://bit.ly/2d3dgames
● $99.99 Level: Hello Coding (170 Hours) http://bit.ly/beginnerscodingcourse
● $149.99 Level: Wall Street Coder http://bit.ly/algorithmicallytrade
● $199 Level: Yearly Unlimited Subscription https://tinyurl.com/lifetimecourses
● $999 Level: Lifetime Unlimited Courses https://tinyurl.com/lifetimecourses
💚 Every dollar we make from these courses we put back into making more content.
💬 After training hundreds of thousands of developers and building a business from scratch, I can ensure your project is a success and your business makes more profit than you can on your own. For my consulting services, email hello(at)mammothinteractive.com.
🔔 Be the first to hear news via our newsletter: https://mailchi.mp/mammothinteractive…
🎓 Sign up to get a free course. Regularly 200 dollars now FREE!
https://bit.ly/2pQtT96
🐘 Check out our website: http://www.mammothinteractive.com
by MammothInteractive
linux web server