简体   繁体   中英

Node.js and Express relationship

I'm trying to understand the connection between Node.js and Express.

My Code for creating a Node.js Server:

const https = require('https');
const fs = require('fs');

const options = {
  key: fs.readFileSync('./https1/key.pem'),
  cert: fs.readFileSync('./https1/cert.pem')
};

const server  = https.createServer(options, function(req,res){
  res.writeHead(200);
  res.end(`Hello world!!!!!!!!!!! \n`);
});

server.listen(3000, function(){
  console.log('Server listening on port 3000 \n');
});

I run a curl operation curl -k localhost:3000 and it gives me a "Hello World" Output

My code for creating an Express Server:

// call the packages we need
var express    = require('express');       
var app        = express();                 
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 8080;

// ROUTES FOR OUR API
var router = express.Router();
// test route to make sure everything is working (accessed at GET    http://localhost:8080/api)
router.get('/', function(req, res) {
    res.json({ message: 'hooray! welcome to our api!' });   
});
app.listen(port);
console.log('Magic happens on port ' + port);

Is it possible for us to mix both of these?

To be more specific, I would like to create my Server using the Node.js way, but create my routes using the Express way. Can I do it or should I just follow one methodology? What is the connection between Node.js and Express? I understand that Express is just a framework for Node.js but where exactly does the deviation occurs if at all any?

Can I mix and combine the two when required?

Thank you

You surely can that's the way to create a Secure HTTPS server with express and followed in most projects

 const https = require('https'); const express = require('express'); const app = express(); const options = { key: fs.readFileSync('./https1/key.pem'), cert: fs.readFileSync('./https1/cert.pem') }; const server = https.createServer(options, app); app.get('/', (req, res) => { res.send('hello world') } server.listen(config.port, () => { console.log(`Express server listening on port ${port} in ${app.get('env')} mode`); }); 

Now add your routes and all.

Yes you can combine nodejs and express , but not encourage you to combine those unless you have specific purpose such as using AWS lambda or making specific OS tasks that has to be made only with pure node.

As you already know, express is just a framework. You can write code more shortly using express .

For example, to make the browser displaying Hello world,

// nodejs version

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

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



// express version

const express = require('express');
const app = express();

const port = 3000;

app.listen(port, (req, res) => {
  res.send('Hello World!\n');
})

More easier, and intuitive.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM