最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you write a vanilla nodejs router without using npm package - Stack Overflow

programmeradmin6浏览0评论

For example I have a server.js

'use strict';
var http, bunyan, config;

config   = require('config');
http     = require('http');
bunyan   = require('bunyan');

//require('./routes/'); ?

process.env.TZ = 'UTC';
process.env.NODE_ENV =  process.env.NODE_ENV || 'staging';
var environment   = config.get('environment');

var log = bunyan.createLogger({
  name        : 'index',
  level       : 'debug',
  stream      : process.stdout,
  });

var server = http.createServer(function (request, response) {
  response.writeHead(200,
  {'Content-Type'   : 'text/plain'});
});

server.on('error', function(error){
  log.error('Errored with the message: ',error);
  process.exit(1);
});

server.listen(environment.port);
log.info(process.env.NODE_ENV);
log.info('Server running at :'+environment.port+'/');

I have three controllers Blog, Todo and Auth It is trivial to do this in restify, express or hapi but how do I write routing file without using any npm packages?

Do I need to write require(./routes)(server) ? and then function routes(server) {} module.exports = routes ?

Is there a more readable way?

For example I have a server.js

'use strict';
var http, bunyan, config;

config   = require('config');
http     = require('http');
bunyan   = require('bunyan');

//require('./routes/'); ?

process.env.TZ = 'UTC';
process.env.NODE_ENV =  process.env.NODE_ENV || 'staging';
var environment   = config.get('environment');

var log = bunyan.createLogger({
  name        : 'index',
  level       : 'debug',
  stream      : process.stdout,
  });

var server = http.createServer(function (request, response) {
  response.writeHead(200,
  {'Content-Type'   : 'text/plain'});
});

server.on('error', function(error){
  log.error('Errored with the message: ',error);
  process.exit(1);
});

server.listen(environment.port);
log.info(process.env.NODE_ENV);
log.info('Server running at http://0.0.0.0:'+environment.port+'/');

I have three controllers Blog, Todo and Auth It is trivial to do this in restify, express or hapi but how do I write routing file without using any npm packages?

Do I need to write require(./routes)(server) ? and then function routes(server) {} module.exports = routes ?

Is there a more readable way?

Share Improve this question asked Apr 27, 2016 at 0:25 Kenichi ShibataKenichi Shibata 1582 silver badges12 bronze badges 4
  • can I ask why? Is it only for learning puposes – omarjmh Commented Apr 27, 2016 at 0:25
  • I wrote a node program without framework, as I don't need it all the other functionalities they offer. but having problems with routes, right now all I have is require('./../controllers/1.js'); – Kenichi Shibata Commented Apr 27, 2016 at 0:31
  • 1.js is not a function but rather an extension code, because I cant figure out how to do routing :D – Kenichi Shibata Commented Apr 27, 2016 at 0:31
  • 1 There are no routes without middleware, you'd have to parse the URL's yourself inside the createServer callback and figure out what to do. – adeneo Commented Apr 27, 2016 at 0:37
Add a ment  | 

1 Answer 1

Reset to default 7

You need to also import url to parse the url, then check the path. From there you can send back what you want, in the code below it is an image from a file:

Note this is basically what adeneo has stated in the ment above!

http.createServer(function (request, response) {
  var path = url.parse(request.url, true).pathname;

  if (request.method === 'POST') {
    if (path === '/hifive') {
      response.writeHead(200,{'Content-Type':'image/jpg'});
      fs.readFile(__dirname + '/assets/hifive.jpg', function(err, data) {
        if (err) console.log(err);
        response.end(data, 'utf-8');
      })
    }
    else {
     response.end(404);
   }
 }
发布评论

评论列表(0)

  1. 暂无评论