sending javascript with node.js code example

Example 1: send email nodejs

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: '[email protected]',
    pass: 'yourpassword'
  }
});

var mailOptions = {
  from: '[email protected]',
  to: '[email protected]',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

Example 2: node js send javascript

const fs = require("fs");
const hostname = "127.0.0.1";
const port = 3000;

// use createReadStream instead to save memory
const index = fs.readFileSync("index.html");
const java = fs.readFileSync("javascript.js");

// this is our request handler
const server = http.createServer((req, res) => {
 if (req.url === "/") {
   res.setHeader("Content-Type", "text/html");
   res.write(index);
 }
 if (req.url === "/javascript.js") {
   res.setHeader("Content-Type", "text/javascript");
   res.write(java);
 }
 res.statusCode = 200;
 //   res.writeHead(200, java);
 res.end();
});

server.listen(port, hostname, () => {
 console.log(`Server running at http://${hostname}:${port}/`);
});