账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    node-windows配置
    55
    0

    使用node-windows配置使得开机自启动node服务,项目是node+express搭建的,使用的es6,用babel编译,平常开启时这样的:

     "scripts": {
        "build": "babel ./bin/www -d lib",
        "start": "nodemon ./bin/www --exec babel-node --presets es2015,stage-0"
      },

    然后node-windows的nw.js是这样的

    let Service = require('node-windows').Service
    
    let svc = new Service({
      name: 'test',
      description: 'test',
      script: './bin/www'
    })
    
    svc.on('install', () => {
      svc.start()
      console.log('install complete.')
    })
    
    svc.install()

    请问有什么方法可以在es6下让node nw.js开启?错误日志如下:

    F:\test\app.js:1
    (function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                                  ^^^^^^
    
    SyntaxError: Unexpected token import
        at createScript (vm.js:56:10)
        at Object.runInThisContext (vm.js:97:10)
        at Module._compile (module.js:542:28)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (F:\test\bin\www:7:11)
        at Module._compile (module.js:570:32)
    F:\test\app.js:1
    (function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                                  ^^^^^^
    
    SyntaxError: Unexpected token import
        at createScript (vm.js:56:10)
        at Object.runInThisContext (vm.js:97:10)
        at Module._compile (module.js:542:28)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (F:\test\bin\www:7:11)
        at Module._compile (module.js:570:32)
    F:\test\app.js:1
    (function (exports, require, module, __filename, __dirname) { import express from 'express'
                                                                  ^^^^^^
    
    SyntaxError: Unexpected token import
        at createScript (vm.js:56:10)
        at Object.runInThisContext (vm.js:97:10)
        at Module._compile (module.js:542:28)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)
        at Function.Module._load (module.js:438:3)
        at require (internal/module.js:20:19)
        at Object.<anonymous> (F:\test\bin\www:7:11)
        at Module._compile (module.js:570:32)
    C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:177
        child.send('shutdown');
             ^
    
    TypeError: Cannot read property 'send' of null
        at process.killkid (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:177:10)
        at emitOne (events.js:96:13)
        at process.emit (events.js:188:7)
        at process.exit (internal/process.js:164:15)
        at monitor (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:99:17)
        at ChildProcess.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-windows\lib\wrapper.js:170:5)
        at emitTwo (events.js:106:13)
        at ChildProcess.emit (events.js:191:7)
        at Process.ChildProcess._handle.onexit (internal/child_process.js:219:12)

    app.js部分:

    import express from 'express'
    import path from 'path'
    import logger from 'morgan'
    
    let app = express()
    global.app = app
    
    //  设置访问请求头,解决访问跨域问题
    app.all('*', (req, res, next) => {
      res.header('Access-Control-Allow-Origin', '*')
      res.header('Access-Control-Allow-Header', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
      res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS')
      res.header('X-Powered-By', '4.15.5')
    
      if(req.method === 'OPTIONS') res.send(200)
      else next()
    })
    
    //  视图模板
    app.set('views', path.join(__dirname, 'views'))
    app.set('view engine', 'jade')
    
    app.use(logger('dev'))
    app.use(express.static(path.join(__dirname, 'public')))
    
    let routes = require('./routes/index')
    //  404
    app.use( (req, res, next) => {
      let err = new Error('Not Found')
      err.status = 404
      next(err)
    })
    
    //  错误处理
    app.use( (err, req, res) => {
     
      res.locals.message = err.message
      res.locals.error = req.app.get('env') === 'development' ? err : {}
    
      res.status(err.status || 500)
      res.render('error')
    })
    
    module.exports = app
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 基尔霍夫电流定律 普通会员 1楼

      在Node.js中,你不需要安装Windows环境就可以使用Node.js,但是为了方便管理你的项目,你需要在Windows上安装Node.js。以下是使用Windows上的命令行安装Node.js的步骤如下:

      1. 下载Node.js。你可以从官方网站下载最新版本的Node.js。

      2. 运行下载好的安装包。在命令行中,你可以输入以下命令:

      npx node -v

      这将显示Node.js的版本号。

      1. 验证安装。在命令行中,你可以输入以下命令:

      node -v

      如果Node.js安装成功,你应该能看到类似于v8.23.0的版本号。

      1. 如果你使用的是全局安装,你需要创建一个新的Node.js项目。打开命令行,然后输入以下命令:

      npx create-react-app my-app

      这将创建一个新的React应用程序。

      1. 进入你的项目文件夹。在命令行中,你可以输入以下命令:

      cd my-app

      这将进入你的React应用程序的文件夹。

      1. 运行你的应用程序。在命令行中,你可以输入以下命令:

      npm start

      这将启动你的React应用程序。

      以上就是在Windows上安装Node.js的基本步骤。如果你遇到任何问题,你可以查阅Node.js的官方文档或在Stack Overflow上寻求帮助。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部